【问题标题】:Android GCM "background" notifications do not launch my app when clicked单击时,Android GCM“背景”通知不会启动我的应用程序
【发布时间】:2016-08-30 16:56:35
【问题描述】:

当我收到“通知”GCM 消息时,android 会自动根据标题和正文创建通知,并将其放入通知阴影中。

这里所说的“通知”是指本文档中称为通知的https://developers.google.com/cloud-messaging/concept-options#notifications_and_data_messages

我遇到的问题是,当我点击它时,什么也没有发生 - 它不会启动我的活动或关闭通知。

我已尝试使用“click_action”选项,但没有效果。 我添加了一个“数据”部分,但没有效果。

当我的应用程序处于前台时,我按预期获得了 onMessageReceived,并且我在其中构建的 NotificationCompat 按预期重新启动应用程序。

我缺少什么来获取启动应用程序的后台通知?

这是清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="example.package"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="21" />

    <!-- [START gcm_permission] -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <!-- [END gcm_permission] -->

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <permission
        android:name="example.package.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="example.package.permission.C2D_MESSAGE" />



    <application
        android:name="example.package.application.appApplication"
        android:allowBackup="true"
        android:icon="@drawable/app_app_icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        tools:replace="android:icon, allowBackup"
        >
        <activity
            android:name="example.package.main.activity.MainActivity"
            android:label="@string/app_name"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- [START gcm_receiver] -->
        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="example.package" />
            </intent-filter>
        </receiver>
        <!-- [END gcm_receiver] -->


        <!-- [START gcm_listener] -->
        <service
            android:name="example.package.service.MyGcmListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <!-- [END gcm_listener] -->
        <!-- [START instanceId_listener] -->
        <service
            android:name="example.package.service.MyInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>
        <!-- [END instanceId_listener] -->
        <service
            android:name="example.package.service.RegistrationIntentService"
            android:exported="false"></service>

        </application>

</manifest>

这是我发送给 GCM 的示例 json:

{
    "to" : "<snipped>",
    "notification" : {
        "title" : "<snipped>",
        "body" : "<snipped>",
        "icon" : "ic_launcher"

    },
"data":{"a":"b"},
"content_available": true,
"click_action":"android.intent.action.MAIN"
}

当我点击通知窗口中的项目时,我唯一得到的详细登录信息是:

08-30 11:54:35.942 3684-3980/? D/InputReader: Input event(4): value=1 when=7306609326000
08-30 11:54:35.942 3684-3980/? D/InputReader: Input event(4): value=1 when=7306609326000
08-30 11:54:35.942 3684-3980/? I/InputReader: Touch event's action is 0x0 (deviceType=0) [pCnt=1, s=0.323 ] when=7306609326000
08-30 11:54:35.952 3684-3980/? D/InputReader: lastThreadEndTime = 7303509457022, currentThreadStartTime = 7303509466814, diff = 0
08-30 11:54:35.952 3684-3979/? I/InputDispatcher: Delivering touch to (4091): action: 0x0, toolType: 1
08-30 11:54:35.952 4091-4091/? D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
08-30 11:54:35.952 3684-4035/? D/lights: button : 1 +
08-30 11:54:35.992 3684-4035/? D/lights: button : 1 -
08-30 11:54:36.112 3684-3980/? D/InputReader: Input event(4): value=0 when=7306778958000
08-30 11:54:36.112 3684-3980/? D/InputReader: Input event(4): value=0 when=7306778958000
08-30 11:54:36.112 3684-3980/? I/InputReader: Touch event's action is 0x1 (deviceType=0) [pCnt=1, s=] when=7306778958000
08-30 11:54:36.112 3684-3979/? I/InputDispatcher: Delivering touch to (4091): action: 0x1, toolType: 1

这发生在 Marshmallow 和 JellyBean 设备上,虽然我没有测试过两者之间的任何东西。

我知道我可以单独发送“数据”元素,即使在后台也可以启动我的服务,但如果应用程序被强制停止,它的缺点是无法工作。我真的想弄清楚我需要做什么才能获得 Google 在该链接上描述的行为。

这是 MyGcmListenerService - 当应用程序在前台时,它会被正确调用。

public class MyGcmListenerService extends GcmListenerService {

    private static final String TAG = "MyGcmListenerService";

    /**
     * Called when message is received.
     *
     * @param from SenderID of the sender.
     * @param data Data bundle containing message data as key/value pairs.
     *             For Set of keys use data.keySet().
     */
    // [START receive_message]
    @Override
    public void onMessageReceived(String from, Bundle data) {
//        String message = data.getString("message");
        String message = data.getBundle("notification").getString("body");
        String customFields = data.getBundle("notification").getString("customFields");
        String title = data.getBundle("notification").getString("title");
        Log.d("From: " + from);
        Log.d( "Title: " + title);
        Log.d( "Message: " + message);
        Log.d( "CustomFields: " + customFields);


        if (from.startsWith("/topics/")) {
            // message received from some topic.
        } else {
            // normal downstream message.
        }

        // [START_EXCLUDE]
        /**
         * Production applications would usually process the message here.
         * Eg: - Syncing with server.
         *     - Store message in local database.
         *     - Update UI.
         */

        /**
         * In some cases it may be useful to show a notification indicating to the user
         * that a message was received.
         */
        sendNotification(message);
        // [END_EXCLUDE]
    }
    // [END receive_message]

    /**
     * Create and show a simple notification containing the received GCM message.
     *
     * @param message GCM message received.
     */
    private void sendNotification(String message) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        /* resumes the current activity - just like pressing link on home page */


        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_ic_notification)
                .setContentTitle("GCM Message")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

为了澄清,我正在专门研究如何让这种行为与 GCM 一起使用:

根据https://developers.google.com/cloud-messaging/downstream '当您的应用程序在后台时,Android 会将带有通知的消息定向到系统托盘。默认情况下,用户点击通知会打开应用启动器。'

我已经能够用 FCM 重写它,我没有看到这个问题,但我真的很想知道我缺少什么。

【问题讨论】:

  • MyGcmListenerService.java 的邮政编码
  • @Bhupendra 我会发布它,但它实际上与谷歌 git 页面上的 gcm 相同。在此过程中,当通知栏中的通知由操作系统创建时,它似乎没有以任何方式被调用。

标签: android android-activity notifications google-cloud-messaging push


【解决方案1】:

基于此related SO question,在 JSON 中,“通知”用于发送简单的通知而无需操作。

据此documentation:

通知为开发人员提供了一种简单的方式来发送用户可见的显示消息,其中包含一些预定义的键和可选的自定义键/值对。数据有效负载仅包括开发人员的自定义键/值对,客户端应用程序必须处理该消息。您可以发送具有通知和数据负载的消息。

这个SO question还建议把消息接收器中的MainActivity.class替换为入口Activity。

final Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

您还可以使用示例代码查看此link,了解单击通知时如何打开应用程序。希望这会有所帮助!

【讨论】:

  • 感谢您的信息,但它不应该没有动作,它应该打开启动器意图。您的第二部分显示了 java 代码,看起来您建议我使用“数据”消息,这是我试图避免的。根据developers.google.com/cloud-messaging/downstreamWhen your app is in the background, Android directs messages with notification to the system tray. A user click on a notification opens the app launcher by default.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
  • 2013-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多