【发布时间】:2016-11-02 21:15:44
【问题描述】:
我正在通过我自己的 web 服务使用 PHP 发送 Firebase 通知,如下所示。
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = SERVER_KEY ',
'Content-Type: application/json'
);
1.当应用程序在前台和后台没有任何问题时,通知将会到来,但是如果我从最近的托盘中删除了应用程序然后通知不来,我必须为此处理任何解决方案吗? (就像 Whatsup 通知显示所有场景,即使我从设置中强制停止此应用)
2.一旦我收到通知,从“onMessageReceived”如何将这些消息、正文内容传递给我的活动/片段?
3.在某些情况下,我想向所有人发送通知,而不是将所有令牌添加到数组中。任何其他方式来处理像“发送给所有人”这样的事情?
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Data Payload : " + remoteMessage.getData());
sendNotification(remoteMessage.getData().get("message"));
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.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.mipmap.ic_launcher)
.setContentTitle("Firebase Push Notification")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
失败的设备:(从最近的托盘中删除后,通知未发送)
- 小米红米 Note 2 (5.0.2 Lollipop)
- 华为荣耀5c(5.1.1 Lollipop)
成功的设备:(从最近的托盘中删除后会发出通知)
- HTC One X (4.2 果冻豆)
- 三星 Galaxy Grand Prime (4.4 Kitkat)
【问题讨论】:
-
我认为这不是操作系统版本的问题(Lollipop vs Jelly Bean 等),更可能是是否允许 Google Play 服务唤醒已被recients解雇。一些制造商调整了这种行为。我建议向 firebase 支持提出问题,以便 FCM 团队可以更深入地调查未按预期运行的设备。 firebase.google.com/support
-
我的数据消息也有同样的问题。据我所知,它不适用于任何版本的 Android。我已经在 Marshmallow 和 Lollipop 上进行了测试。
-
熔岩爱尔兰 x1 也
标签: android firebase notifications android-notifications firebase-cloud-messaging