当应用程序关闭时,fcm通知的某些功能不起作用

时间:2018-08-26 11:45:23

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

我正在使用fcm进行推送通知。 当应用打开并收到通知时,通知可以正常工作。 但是当应用程序关闭并收到通知时,振动,声音,添加动作等功能将无法正常工作。

Intent intent = new Intent(this, FullscreenActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    String urlAddress = "https://cafebazaar.ir/";
    Intent intent2 = new Intent(Intent.ACTION_VIEW, Uri.parse(urlAddress));
    intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent2.setPackage("com.farsitel.bazaar");
    PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0, intent2, PendingIntent.FLAG_ONE_SHOT);

    Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Notification notificationBuilder = new Notification.Builder(this)
                .setSmallIcon(R.drawable.macanads)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setColor(Integer.parseInt(color))
                .setSound(soundUri)
                .setVibrate(new long[] { 1000, 1000, 1000})
                .addAction(android.R.drawable.ic_menu_directions,"بروزرسانی",pendingIntent2)
                .setContentIntent(pendingIntent).build();
    }


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

    notificationManager.notify(0, notificationBuilder);
}

1 个答案:

答案 0 :(得分:0)

我的问题已解决。您不得在对Firebase API的请求中放入JSON密钥“ notification”,而应使用“ data”

在以下情况下,使firebase库调用onMessageReceived()

前台应用

后台应用

应用已被杀死

您不得在对Firebase API的请求中放入JSON密钥“ notification”,而应使用“ data”,请参见下文。

当您的应用程序在后台或被杀死时,以下消息不会调用您的onMessageReceived(),并且您无法自定义通知。

相关问题