Android GCM 通知返回堆栈

标签 android notifications push-notification google-cloud-messaging back-stack

我在 Android 上设置了 GCM,并且能够正确接收通知。在通知中,我设置了“click_action”,以便 android 在选择通知时为我打开一个特定的 Activity (ListActivity)。这一切都很好。

但是,如果用户通过按返回键离开应用程序,则当新 Activity 启动时(通过 gcm)整个返回堆栈都将丢失。这是我的 android list ,也是我如何发送通知的示例。

AndriodManifest.xml

<activity
    android:name=".views.HomeActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>

<activity
    android:name=".views.ListActivity"
    android:parentActivityName=".views.HomeActivity"
    android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="OPEN_ACTIVITY_LIST"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".views.HomeActivity"/>
</activity>

通知示例

curl -X POST https://gcm-http.googleapis.com/gcm/send \
-H "Content-Type:application/json" \
-H "Authorization:key=AUTHORIZATION_KEY" \
-d '{
  "to" : "GCM_TOKEN",
  "priority" : "high",
  "notification" : {
    "body" : "Notification body",
    "title" : "Notification Title",
    "click_action": "OPEN_ACTIVITY_LIST"
  },
  "data": {
    "message": "Please open an activity with a backstack configured!"
  }
}'

是否可以让 gcm 自动为我创建返回堆栈?如果是这样,我该如何实现?

最佳答案

我发现这很有趣 website .看看Step 5 .我想这就是您要找的。

To allow a cloud message to open the app and jump straight to an Intercom message we need to get the intent data sent to us. In your apps launch activity (the first activity it will go to) add the following. You should call this method early in the Activity life-cycle such as onCreate.

Intercom.client().openGCMMessage(getIntent());

If you wish to create a custom back stack for this notification you can pass a TaskStackBuilder to the same method:

openGCMMessage(Intent intent, TaskStackBuilder stackBuilder);

关于Android GCM 通知返回堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35588109/

相关文章:

android - 为什么集成React Native时compileSdkVersion错误?

android长时间运行的服务没有通知

ios - 如何在 iOS 中监听传入的短信事件

iphone - 有没有办法在没有的情况下测试 Android、iPhone 推送通知?

java - Android 在循环后将所有对象添加到列表中

Android 显示全屏 slider 图像

java - 不将值从推送通知传递给 Activity

android - 手机间隙 : how to play sound on android for a push notification

android - 如何在我的 Android 应用程序中使用 django 身份验证(django.contrib.auth)

jenkins - 构建恢复时如何在 Jenkins 管道中发送通知?