Firebase Cloud Messaging(FCM)-用户单击带有附加内容的通知时的启动活动 [英] Firebase Cloud Messaging (FCM) - Launch Activity when user clicks the notification with extras

查看:93
本文介绍了Firebase Cloud Messaging(FCM)-用户单击带有附加内容的通知时的启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在用户单击通知时(当应用程序在后台运行时)打开一些特定参数的特定活动.我正在使用click_action,并且工作正常,该应用会打开所需的活动.

I'm trying to open a specific activity when the user clicks the notification, when the app is in the background, with some extra parameters. I'm using the click_action and it's working fine, the app opens the desired Activity.

现在,我需要服务器向此Activity传递一个额外的参数id,以便我可以呈现与通知关联的所需详细信息.就像电子邮件应用程序一样,当我们单击通知时,将打开该特定电子邮件的详细信息.

Now I need the server to pass an extra parameter, an id, to this Activity so I can present the desired details associated with the notification. Like an e-mail application, that when we click on the notification opens the details of that specif email.

我该怎么做?

推荐答案

好,我找到了解决方法.

Ok, I have found the solution.

这是我从服务器发送到应用的json

This is the json that I'm sending from the server to the app

{
  "registration_ids": [
    "XXX",
    ...
  ],
  "data": {
    "id_offer": "41"
  },
  "notification": {
    "title": "This is the Title",
    "text": "Hello I'm a notification",
    "icon": "ic_push",
    "click_action": "ACTIVITY_XPTO"
  }
}

在AndroidManifest.xml

At the AndroidManifest.xml

<activity
    android:name=".ActivityXPTO"
    android:screenOrientation="sensor"
    android:windowSoftInputMode="stateHidden">
    <intent-filter>
        <action android:name="ACTIVITY_XPTO" />        
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

当应用关闭或在后台运行并且用户单击通知时,它将打开我的ActivityXPTO,以检索id_offer我只需要这样做

When the app is closed or in background and the user clicks on the notification it opens my ActivityXPTO, to retrieve the id_offer I only need to do

public class ActivityXPTO extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ...

        String idOffer = "";

        Intent startingIntent = getIntent();
        if (startingIntent != null) {
            idOffer = startingIntent.getStringExtra("id_offer"); // Retrieve the id
        }

        getOfferDetails(idOffer);
    }

    ...
}

就这样...

这篇关于Firebase Cloud Messaging(FCM)-用户单击带有附加内容的通知时的启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆