【发布时间】:2016-09-29 18:55:06
【问题描述】:
EDIT3: 好的,我发现了问题:如果应用程序被终止,发送data 和 notification 不会触发onMessageReceived。如果目标设备是android,notification需要设置为null。
这真是一种愚蠢的行为。
原帖:
当应用启动时,我可以成功地从notificationBuilder 传递给活动的意图中检索捆绑包。
但是,如果我终止应用程序,通知仍然有效,但意图中的 GetExtras() 为空。
在我的FirebaseMessagingService 子类中:
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("key_1","value");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//also tried with PendingIntent.FLAG_CANCEL_CURRENT
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, notificationBuilder.build());
}
当应用程序被杀死时,firebaseMessagingService 甚至无法写入文件。它不会崩溃,它会构建并显示通知,但对磁盘所做的任何操作都不起作用。
编辑:我已将onMessageReceived 方法的内容替换为如下:
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
// Do nothing
}
当应用程序运行时,什么也没有发生 - 正如预期的那样 - 但是当应用程序被终止时,会显示一条通知,其中名称为 app 作为标题,remoteMessage.getNotification().getBody() 作为内容文本。
似乎onMessageReceived 没有被解雇。而是调用了另一个魔术函数...
EDIT2:根据要求,这是从服务器发送的:
{
"data":
{
"form_id":"33882606580812",
"recipientUserId":10500
}
,
"to":"e0bU2jIVO9g:APA91bHsS-s3G1gQLcTFtRUC77pJUWcZvD7h9NfUgFLD-bFam1S0dddngVcmrQlXR5i6DfTsc69T8JbIrSuzyF1iv0c4ac1gmkwvGVMwZo_yA4KwOh82Nx-weYeL79r79si4qH3QBEgs",
"notification":
{
"body":"you have a new notification",
"badge":"1",
"sound":"default"
},
"delay_while_idle":false
}
【问题讨论】:
-
请您分享您用于发送数据和通知的 fcm 的 JSON 吗?
-
你也把所有这些代码都放在这里了吗?
// Check if message contains a data payload. if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Message data payload: " + remoteMessage.getData()); //and your other code i.e intents and all } -
你能告诉我你测试这个的手机的制造商或型号吗?因为许多制造商(尤其是中国制造商)在其架构中都有奇怪的后台服务终止应用程序,这会导致服务后台应用程序出现很多问题等等。只是有点担心,即使这可能是一个问题。
-
您可能有一点,我正在使用 Visual Studio 模拟器进行测试。我打算用真机测试。
-
重复第一条评论,您能否使用您发送给 FCM 的有效负载扩展问题?这一点非常重要,因为 Android 客户端的行为会根据您包含的有效负载类型(数据和/或通知)而有所不同。
标签: android android-intent firebase firebase-notifications