【问题标题】:Firebase Cloud Messaging (FCM) - Launch Activity when user clicks the notification with extrasFirebase Cloud Messaging (FCM) - 当用户点击带有附加功能的通知时启动活动
【发布时间】:2016-09-30 15:22:43
【问题描述】:

当用户单击通知时,当应用程序在后台运行时,我试图打开一个特定的活动,并带有一些额外的参数。我正在使用click_action,它工作正常,应用程序打开了所需的活动。

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

我该怎么做?

【问题讨论】:

  • 你能分享一些代码吗?我的应用程序中有类似的东西,但我使用的是 GCM。在接收通知的服务中,我只需在 Intent 中添加一个额外的字符串,然后使用它来查看我应该打开哪个片段。
  • 我不需要任何帮助,我的应用可以运行 :) 对你有好处,你已经修复了它
  • 你解决了吗?我正在获取相同的问题。我启动了 targetActivity 但没有从意图中得到额外的东西。你有什么解决办法吗?

标签: android android-notifications firebase-cloud-messaging firebase-notifications


【解决方案1】:

好的,我找到了解决办法。

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

{
  "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 中

<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 我只需要这样做

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

    ...
}

就是这样……

【讨论】:

  • 感谢您的精彩回答。使用你的方式我开始了 targetActivity 但没有从意图中获得额外的东西。你有解决办法吗?
  • 嗨@Md.SajedulKarim,您有从服务器发送的json 示例吗?您必须在 json 的“数据”对象中添加额外内容。
  • 这对我也有用。如果我想打开一个特定的页面,那么我必须做什么。看我的问题stackoverflow.com/questions/44797486/…
【解决方案2】:

在 Intent 中添加附加信息,用于启动 Activity,并在方法 onCreate 中的 Activity 中使用 getIntent().getExtras() 来使用它们。例如:

开始活动:

Intent intent = new Intent(context, TargetActivity.class);
Bundle bundle = new Bundle();
bundle.putString("extraName", "extraValue"); 
intent.putExtras(bundle);
startActivity(intent); 

活动中

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle bundle = getIntent().getExtras();
    String value = bundle.getString("extraName");
    ....
}

【讨论】:

  • 开始活动的不是我。这由系统处理。应用处于后台或关闭状态。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-16
  • 1970-01-01
  • 1970-01-01
  • 2012-04-28
  • 1970-01-01
相关资源
最近更新 更多