gpt4 book ai didi

java - 无法通过firebase-cloud-messaging中的pendingIntent传递数据

转载 作者:行者123 更新时间:2023-11-30 05:41:40 24 4
gpt4 key购买 nike

我正在使用 firebase-cloud-messaging 将通知推送到我的应用程序。我希望当用户单击通知时,它会打开 OpenWeb 类和 Intent 打开 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>

当我单击通知时, Activity 启动,OpenWeb 中的 getIntent.getStringExtra("link")null。这意味着 MyFirebaseMessagingService 类不会将数据发送到 Activity。

如何在 OpenWeb 上发送数据?

最佳答案

代码有一些问题1. 在您的 php 脚本中,您没有指定任何 "body" 但您仍在调用 String body = remoteMessage.getNotification().getBody(); 那是一个空指针异常。

2.此 click_action 键仅在应用程序处于前台时才起作用,对于应用程序处于后台并关闭时的所有其他情况,它不起作用。

3.您需要设置标志来启动新 Activity ,因此 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

因此,如果待处理的意图正在触发,由于第 2 个原因,您最终会得到一个 npe,现在您可以通过发送数据消息而不是处理您的通知的 firebase sdk 来解决此问题,然后它将是 NONNULL

好吧,firebase 有两种类型的消息

  1. 由 firebase SDK 处理的推送消息,即您设置了标题和正文以及其他因素但无法发送自定义数据的消息

data masseages has to be key-value pairs though.

举个例子,您想要发送一些数据以及客户端应用程序将处理的通知,例如您要发送一些有关体育比赛的新闻,这样您就可以创建一个 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"); 来获取标题,对于所有其他内容也同样如此,例如bodycoolNews..

现在,对于 BroadCast 接收者,您可以通过 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);

并启动广播类

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);

就是这样。

抱歉,有错别字。

关于java - 无法通过firebase-cloud-messaging中的pendingIntent传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55518224/

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