在后台或应用关闭时未收到Firebase通知

时间:2017-05-01 07:16:24

标签: android firebase push-notification firebase-cloud-messaging

应用程序在关闭或后台时不会收到通知。下面是我的服务类,它处理通知。 仅当唯一的应用程序正在运行时,通知才有效。

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFMService";
    Random random = new Random();
    int NOTIFICATION_ID = random.nextInt(9999 - 1000) + 1000;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        sendNotification();


    }

    private void sendNotification() {
        Intent intent = new Intent(this, NotificationDisplay.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_bouddha_icon)
                .setContentTitle("Bouddha Meridian School")
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
    }
}

2 个答案:

答案 0 :(得分:0)

您下载了google-service.json并放置在app文件夹中吗? 请参阅此示例以供参考:http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

答案 1 :(得分:0)

当应用程序处于后台时,messagerecieve类将不会命中,因此您必须在将有效负载发送有效负载模拟器时从服务器进行处理。

{
    "to" : "zzzzz",
     "notification": {
     "body": "",
     "title": "",
    "icon": ""
},
"data" : {
 "name" : ""

}
  }