我正在使用firebase-cloud-messaging
向我的应用程序发送推送通知。我希望当用户点击通知时,它会打开OpenWeb类和一个Intent打开的URL。
$data = array("notification"=> array("title"=>"http://example.com/",
"text"=>"This is a test msg!",
"click_action"=>"OpenWeb"),
"priority" => "high",
"to"=>"/topics/main");
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
String click_action = remoteMessage.getNotification().getClickAction();
Context context = getApplicationContext();
Intent intent = new Intent(click_action);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("link",title);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 , intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notification)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.notification))
.setContentTitle("Notification")
.setContentText(body)
.setSound(defaultSoundUri)
.setAutoCancel(true)
.setContentIntent(contentIntent);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 , notificationBuilder.build());
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(getIntent.getStringExtra("link"));
startActivity(intent);
}
<activity android:name=".OpenWeb">
<intent-filter>
<action android:name="OpenWeb" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
当我点击通知时,活动开始,getIntent.getStringExtra("link")
中的OpenWeb
为空。这意味着MyFirebaseMessagingService类不会将数据发送到活动。
我该怎么做才能在OpenWeb
上发送数据?
那么代码有一些问题1.在你的php脚本中,你没有指定任何"body"
,但你仍然在调用String body = remoteMessage.getNotification().getBody();
,这是一个Null指针异常。
2.此click_action键仅在应用程序位于Foreground时才有效,对于所有其他情况,当应用程序处于Background并关闭时,它不起作用。
3.你需要设置标志才能开始新的活动,所以intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
因此,如果由于原因2而未决意图被解雇你最终会被npe结束,现在你可以通过发送数据消息而不是处理你的通知的firebase sdk来做到这一点,然后它将走廊是NONNULL
好的firebase有两种类型的消息
数据主体必须是键值对。
例如,您希望发送一些数据,其中包含客户端应用程序将处理的通知,例如您正在发送有关体育比赛的新闻,因此您可以创建一个JSON格式的节点
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 recevier
,您可以通过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);
并发起broadcast class
和
DONT FORGET TO ADD TO YOUR MANIFEST!
像这样的东西......
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);
就是这样。
抱歉打字错误..