首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在防火墙云消息传递中通过pendingIntent传递数据

无法在防火墙云消息传递中通过pendingIntent传递数据
EN

Stack Overflow用户
提问于 2019-04-04 14:17:23
回答 1查看 1.1K关注 0票数 1

我使用firebase-cloud-messaging对我的应用程序进行推送通知。当用户单击通知时,我希望它打开OpenWeb类和意图打开url。

使用PHP发送数据:

代码语言:javascript
复制
$data = array("notification"=> array("title"=>"http://example.com/",
                                 "text"=>"This is a test msg!",
                                 "click_action"=>"OpenWeb"),
          "priority" => "high",
          "to"=>"/topics/main");

MyFirebaseMessagingService.java

代码语言:javascript
复制
 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

代码语言:javascript
复制
@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

代码语言:javascript
复制
<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上发送数据?

EN

回答 1

Stack Overflow用户

发布于 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有两种类型的消息

  1. 推送由firebase处理的消息,即设置标题、正文和其他因素的消息,但不能发送自定义数据。

不过,数据按摩必须是键值对。

举个例子,您想要发送一些数据以及客户端应用程序将处理的通知,例如,您正在发送一些有关体育比赛的消息,这样您就可以创建一个JSON格式的节点,例如

代码语言:javascript
复制
data: { 
coolNews: "Some Cool News1",
coolNews2: "Some really cool news2"
}

然后你可以打电话给remoteMessage.getData().get("coolNews");

但是,要小心,因为如果您发送以下JSON

代码语言:javascript
复制
Notification:{
to: //UserToken , 
title: "Some Title",
body: "Some body",
data: { 
coolNews: "Some Cool News1",
coolNews2: "Some really cool news2"
}}

只有在客户端打开通知之后才会管理数据有效负载,但是,如果将整个通知json作为数据有效负载发送,并且您处理客户端上的所有内容,则所有通知都将同时收到。如下所示:

代码语言:javascript
复制
 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 recevier,可以通过firebasemessagingId发送广播,如下所示:

代码语言:javascript
复制
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!

就像这样..。

代码语言:javascript
复制
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);

仅此而已。

很抱歉打字..。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55518224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档