本文介绍了onMessageReceived从未使用Android中的Firebase从服务器调用通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Android中的Firebase发送服务器通知。我的代码在从Firebase控制台发送通知时起作用,但在从我的服务器发送消息时不起作用。我打开了调试,并且分析了FirebaseMessagingService的 onMessageReceived
方法从未被调用过。
这是我的FirebaseMessagingService代码:
public class FirebaseMessagingService扩展了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){
意图意图=新意图(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推送通知)
.setContentText(message )
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
$ b $ p
$ b这里是我的PHP代码的服务器端公共函数sendPushNotificationToGCMSever($ token,$ message){
// include_once'config.php'; $ p
$ 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);
返回$ result;
}
有谁能告诉我我做错了什么?任何帮助将不胜感激。谢谢:)
解决方案
表示(发送像控制台这样的通知)你的 $ fields
应该是(如果<$
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 'to'=> $ token,
'notification'=>数组(
'body'=> $ message
),
);
您也可以通过设置'icon'= > 'res / drawables中某个文件的名称'
,如
另请参阅指南中的仅限数据邮件和通知之间的区别
如果您只尝试数据消息并且 onMessageReceived
,那么您可能忘记添加(如)
< service
android:name =。MyFirebaseMessagingService>
< intent-filter>
< action android:name =com.google.firebase.MESSAGING_EVENT/>
< / intent-filter>
< / service>
AndroidManifest.xml
I'm sending notification from server using Firebase in Android. My code works when I send a notification from Firebase Console, but it doesn't work when I send a message from my server. I turned on debugging and I analyzed that onMessageReceived
method of FirebaseMessagingService is never called.
Here is my FirebaseMessagingService code:
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());
}
}
Here is my PHP code of server side"
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;
}
Can anyone tell me what I'm doing wrong? Any help would be much appreciated. Thank you :)
解决方案
The guide says that (to send a notification like the console does) your $fields
should be (if $message
is a string)
$fields = array(
'to' => $token,
'notification' => array(
'body' => $message
),
);
You can also choose the icon for the notification by setting 'icon' => 'the name of some file in res/drawables'
as described in this blog post
Also see the difference between data only messages and notifications in the guide.
If you try data only messages and onMessageReceived
, then you possibly forgot to add (as mentioned in the guide)
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
AndroidManifest.xml
这篇关于onMessageReceived从未使用Android中的Firebase从服务器调用通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!