当应用在前台运行时,我不会收到推送通知



所以我正在制作一个应用程序来获取通知,但它仅在我关闭应用程序时收到通知,但是当我尝试从 FCM 发送通知时,应用程序处于前台时,它不会收到任何通知,并且它崩溃了。怎么了?谁能帮忙

这是我的代码

public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
public MyFirebaseMessagingService() {
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Check if message contains data payload
if (remoteMessage.getData().size() > 0) {
String title = remoteMessage.getNotification().getTitle();//get title
String body = remoteMessage.getNotification().getBody();//get body
String click_action = remoteMessage.getNotification().getClickAction(); //ge
sendNotification(title, body, click_action);
}
//Check if message contains a notification payload
if (remoteMessage.getNotification() != null) {
String title = remoteMessage.getNotification().getTitle();//get title
String body = remoteMessage.getNotification().getBody();//get body
String click_action = remoteMessage.getNotification().getClickAction(); //ge
sendNotification(title, body, click_action);

}
}
private void sendNotification(String title,String messageBody, String click_action) {
Intent intent;
if(click_action.equals("News")){
intent = new Intent(this, NewsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
else if(click_action.equals("Updates")){
intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}else{
intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

看看你是如何检查和接收你的数据:

//Check if message contains data payload
if (remoteMessage.getData().size() > 0) {
String title = remoteMessage.getNotification().getTitle();//get title
String body = remoteMessage.getNotification().getBody();//get body
String click_action = remoteMessage.getNotification().getClickAction(); //ge
sendNotification(title, body, click_action);
}

将其替换为remoteMessage.getData().

崩溃的原因:您检查了getData()是否不为空。在前台onMessageReceived方法中将同时接收(通知和数据有效负载((它不会崩溃,因为您仍然可以收集getNotification()数据(。 在后台,通知有效负载不可用并直接发送到系统托盘,请参阅 https://firebase.google.com/docs/cloud-messaging/android/receive

您的代码应如下所示:

//Check if message contains data payload
if (remoteMessage.getData().size() > 0) {
String title = remoteMessage.getData().get("title");//get title
String body = remoteMessage.getData().get("body");//get body
String click_action = remoteMessage.getData().get("click_action"); //get action, maybe you have to debug this and look for the right key
sendNotification(title, body, click_action);
}

是否已将服务添加到清单中?

<service
android:name=".MyFcmListenerService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>

请参阅迁移指南,了解您是否已完成所有必要的操作

相关内容

  • 没有找到相关文章

最新更新