Nir*_*rel 6 android firebase firebase-notifications
我正在尝试通过新的Firebase推送通知服务向我的用户发送一些数据.
我正在使用"消息文本" - "dbupdate"添加一些"键值"自定义数据项,以检测它是更新消息还是我要向用户显示的正常推送通知.
在我的接收器中,我检查我是否有自定义数据并运行更新,如果为真.如果不是这种情况,我会通过短信显示正常通知.
它仅在我的应用程序打开时有效.如果应用程序关闭,则无论如何都会显示通知,而我的if检查甚至没有运行,
一些代码:
public class PushMessageService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Map<String,String> data = remoteMessage.getData();
String link = data.get("link");
String name = data.get("name");
if (link!=null) {
Log.d("link",link);
Log.d("name",name);
UpdateHelper.Go(this, link, name);
}
else {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_shop_white_24dp)
.setAutoCancel(true)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody());
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(5, mBuilder.build());
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里举例说明
为什么会这样?即使应用程序关闭,我该怎么做才能运行我的代码?
His*_*eer 25
根据我对Firebase推送的经验:
问题:通过"Firebase控制台"发送推送通知时,仅在应用程序位于前台时才会收到通知.如果应用程序被终止或在后台运行,则通知仅在版本> = lollipop中显示,并且通知图标不正确和/或在应用程序中打开默认启动器活动,即使您已在PendingIntent中定义了不同的活动.
根据Firebase文档,推送消息分为2个部分,他们称之为: Docs链接在这里
有两种方法可以发送"推送"消息.
1. Firebase控制台
2.消息传递API - 显示使用高级REST客户端的请求的图像
在onMessageReceived(...)
方法中接收推送消息.如果应用程序处于后台,onMessageReceived(...)
则不会调用Firebase 方法,或仅在通过Firebase控制台发送邮件时才会终止该方法.
如果您通过API发送消息,它可以正常工作.onMessageReceived(...)
无论app是在后台,前台还是已杀死,都会以方法传递消息.(注意:这个工作正常>=Lollipop
- Android 4.4或更低版本的行为仍然无法预测.
更新:
您可以从API发送推送消息(firebase将其称为下游消息),如下所示:https://firebase.google.com/docs/cloud-messaging/downstream
为了解决或"定位"下游消息,应用服务器使用接收客户端应用的注册令牌设置为.您可以使用预定义字段或自定义数据消息发送通知消息; 有关有效负载支持的详细信息,请参阅消息负载中的通知和数据.此页面中的示例显示如何在HTTP和XMPP协议中发送数据消息.
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
Run Code Online (Sandbox Code Playgroud)