当应用程序在前台运行时,我没有收到推送通知

时间:2018-07-04 14:27:04

标签: android firebase firebase-cloud-messaging

所以我正在制作一个要获取通知的应用程序,但是它仅在我关闭该应用程序时得到通知,但是当我尝试从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());
}

2 个答案:

答案 0 :(得分:0)

看看您如何检查和接收数据:

//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);

    }

答案 1 :(得分:0)

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

.spec.strategy

see the migration guide to see that you completed all necessary actions