gpt4 book ai didi

android - Firebase Messaging - 在应用程序处于后台时创建抬头显示

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:06:14 24 4
gpt4 key购买 nike

使用 FCM,当应用程序处于后台或未运行时,我会在系统托盘中收到推送通知。当应用程序位于前台时,我可以覆盖 onMessageReceived 并使用 NotificationCompat 创建我自己的提醒通知。

有没有办法在我的应用程序处于后台或未运行时创建提醒通知?

谢谢

编辑:这里的引用信息是我通过 curl 发送到 https://fcm.googleapis.com/fcm/send

的消息负载
{
"to":"push-token",
"content_available": true,
"priority": "high",
"notification": {
"title": "Test",
"body": "Mary sent you a message!",
"sound": "default"
},
"data": {
"message": "Mary sent you a Message!",
"notificationKey":"userID/notification_type",
"priority": "high",
"sound": "default"
}
}

最佳答案

我找到了解决方案:我只是从本地服务器发送到 firebase 服务器的 json 中删除通知标记,然后在 MyFirebaseMessagingService 中生成回调:onMessageReceived() 方法。在这种方法中,我使用 NotificationCompat.Builder 类生成本地通知。这是安卓的代码:

private void sendNotification(RemoteMessage remoteMessage) {
Intent intent = new Intent(this, SplashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(AppConstant.PUSH_CATEGORY, remoteMessage.getData().get("category"));
intent.putExtra(AppConstant.PUSH_METADATA, remoteMessage.getData().get("metaData"));
intent.putExtra(AppConstant.PUSH_ACTIVITY, remoteMessage.getData().get("activity"));
intent.putExtra(AppConstant.PUSH_ID_KEY, remoteMessage.getData().get("_id"));

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(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("body"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent);

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

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

关于android - Firebase Messaging - 在应用程序处于后台时创建抬头显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39682120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com