Firebase Cloud Messaging (FCM) - Launch Activity when user clicks the notification with extras(Firebase Cloud Messaging (FCM) - 当用户点击带有附加功能的通知时启动活动)

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

问题描述

我试图在用户单击通知时打开特定活动,当应用程序在后台时,使用一些额外的参数.我正在使用 click_action 并且它工作正常,应用程序会打开所需的 Activity.

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) - 当用户点击带有附加功能的通知时启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!