【发布时间】:2016-10-05 09:41:53
【问题描述】:
我正在使用 Android 中的 Firebase 从服务器发送通知。当我从 Firebase 控制台发送通知时,我的代码有效,但当我从服务器发送消息时,它不起作用。我打开调试,分析出FirebaseMessagingService的onMessageReceived方法从来没有被调用过。
这是我的 FirebaseMessagingService 代码:
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private static final String TAG = FirebaseMessagingService.class.getSimpleName();
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
showNotification(remoteMessage.getData().get("message"));
}
private void showNotification(String message){
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Firebase Push Notification")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
这是我的服务器端PHP代码"
public function sendPushNotificationToGCMSever($token, $message){
//include_once 'config.php';
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $token,
'notification' => $message
);
$headers = array(
'Authorization:key=' . SERVER_KEY,
'Content-Type:application/json'
);
$ch = curl_init();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
谁能告诉我我做错了什么?任何帮助将非常感激。谢谢你:)
【问题讨论】:
-
你的应用是在前台还是后台?
标签: php android firebase firebase-cloud-messaging