Java Can';t通过firebase云消息中的PendingEvent传递数据
我正在使用Java Can';t通过firebase云消息中的PendingEvent传递数据,java,firebase,firebase-cloud-messaging,android-pendingintent,Java,Firebase,Firebase Cloud Messaging,Android Pendingintent,我正在使用firebase云消息向我的应用程序推送通知。我希望当用户单击通知时,它会打开OpenWeb类和一个意图打开的url 使用PHP发送数据: $data=array(“通知”=>array(“标题”=>)http://example.com/", “text”=>“这是一个测试消息!”, “单击操作”=>“OpenWeb”), “优先级”=>“高”, “至”=>“/topics/main”); MyFirebaseMessagingService.java OpenWeb.java @
firebase云消息
向我的应用程序推送通知。我希望当用户单击通知时,它会打开OpenWeb类和一个意图打开的url
使用PHP发送数据:
$data=array(“通知”=>array(“标题”=>)http://example.com/",
“text”=>“这是一个测试消息!”,
“单击操作”=>“OpenWeb”),
“优先级”=>“高”,
“至”=>“/topics/main”);
MyFirebaseMessagingService.java
OpenWeb.java
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Intent Intent=newintent(Intent.ACTION_视图,Uri.parse(getIntent.getStringExtra(“链接”));
星触觉(意向);
}
AndroidManifest.xml
当我单击通知时,活动开始,并且OpenWeb
中的getIntent.getStringExtra(“链接”)
为null。这意味着MyFirebaseMessagingService类不会将数据发送到活动
我应该怎么做才能在
OpenWeb
上发送数据呢?代码有一些问题
1.在php脚本中,您没有指定任何“body”
,但仍在调用字符串body=remoteMessage.getNotification().getBody();
这是一个空指针异常
2.只有当应用程序位于前台时,此点击操作键才会起作用,对于所有其他情况,当应用程序位于后台并关闭时,此键不起作用
3.您需要设置标志以启动新活动,因此intent.addFlags(intent.FLAG\u activity\u new\u TASK);
因此,如果由于第2个原因,pending intent正在触发您的npe,现在您可以通过发送数据消息而不是firebase sdk来处理您的通知来解决此问题,然后它将成为NONNULL
好的,firebase有两种类型的消息
data: {
coolNews: "Some Cool News1",
coolNews2: "Some really cool news2"
}
然后您可以调用remoteMessage.getData().get(“coolNews”);
但是要小心,因为如果您发送以下JSON
Notification:{
to: //UserToken ,
title: "Some Title",
body: "Some body",
data: {
coolNews: "Some Cool News1",
coolNews2: "Some really cool news2"
}}
数据有效负载只有在客户端打开通知后才会被管理,但是,如果您将整个通知json作为数据有效负载发送,并且在客户端处理所有内容,那么所有内容都将同时被接收。
例如:
Notification:{
to: //UserToken
data: {
title: "Some Title",
body: "Some body",
coolNews: "Some Cool News1",
coolNews2: "Some really cool news2"
}}
然后在FirebaseMessageId
中,您可以调用remotemessage.getData().get(“title”);
来获取标题,对所有其他内容(如body
和coolNews
)也可以调用
现在,对于BroadCast receiver
,您可以通过firebasemessagingId
发送广播,如下所示:
Intent intent = new Intent(this, NotificationBR.class);
Bundle bundle = new Bundle();
bundle.putDouble("key1",remoteMessage.getData().get("title"));
bundle.putDouble("key2",remoteMessage.getData().get("coolNews1"));
......
intent.putExtras(bundle);
sendBroadcast(intent);
并启动广播类
和
别忘了添加到您的清单中!
像这样的
public class NotificationBR extends BroadCastReciever {
@Override
public void onReceive(Context ctx, Intent intent){
if(intent.getExtras() != null){
String key1 = intent.getStringExtra("key1");
......
}
//Then you can use intent to send it to whichever activity you want
Intent sendToActivity = new Intent(ctx, Activity.class);
sendToActivity.putExtras(someBundle) //Containing your strings you want to pass to activity, you can also use sendToActivity.putStringExtra("Some values")
startActivity(sendToActivity);
就这样
抱歉,输入错误。您能解释更多关于数据信息或广播接收器的信息吗?@Daniel兄弟,请检查一下,如果您觉得有用,请接受答案。
public class NotificationBR extends BroadCastReciever {
@Override
public void onReceive(Context ctx, Intent intent){
if(intent.getExtras() != null){
String key1 = intent.getStringExtra("key1");
......
}
//Then you can use intent to send it to whichever activity you want
Intent sendToActivity = new Intent(ctx, Activity.class);
sendToActivity.putExtras(someBundle) //Containing your strings you want to pass to activity, you can also use sendToActivity.putStringExtra("Some values")
startActivity(sendToActivity);