我使用firebase-cloud-messaging对我的应用程序进行推送通知。当用户单击通知时,我希望它打开OpenWeb类和意图打开url。
使用PHP发送数据:
$data = array("notification"=> array("title"=>"http://example.com/",
"text"=>"This is a test msg!",
"click_action"=>"OpenWeb"),
"priority" => "high",
"to"=>"/topics/main");MyFirebaseMessagingService.java
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());
}OpenWeb.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(getIntent.getStringExtra("link"));
startActivity(intent);
}AndroidManifest.xml
<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上发送数据?
发布于 2019-04-04 14:39:02
代码1有一些问题。在php脚本中,您没有指定任何"body",但仍然调用String body = remoteMessage.getNotification().getBody();,这是一个Null指针异常。
2.只有当应用程序处于前台时,此click_action键才能工作;对于所有其他情况,当应用程序处于后台并关闭时,它不能工作。
3.您需要设置标志来启动一个新的活动,以便intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
因此,如果挂起的意图是激发您的npe,因为原因2,现在您可以这样做,通过发送数据消息,而不是用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);仅此而已。
很抱歉打字..。
https://stackoverflow.com/questions/55518224
复制相似问题