【问题标题】:GcmListenerService.onMessageReceived() called only when app is runningGcmListenerService.onMessageReceived() 仅在应用程序运行时调用
【发布时间】:2016-03-13 06:39:45
【问题描述】:

我正在尝试实施 GCM。一切正常,除了 onMessageReceived() 方法仅在应用程序运行时被调用。

我搜索了解决方案,但找不到与我匹配的案例。我对 GCM 的请求正文仅包含“to”和“data”部分,而不包含“notification”部分。

这是发送到 GCM 的请求正文:

{"data":{"title":"Test","message":"Test Message"},"to":"ffl3Q260K8E:APA91bG90Ra_CyXdmB4ztcCXYKEMKFliZ_MVpkqYnzUW2Xizcem4iknSt9guKDeEbWYl2YwwEKa7kKVllE0mBRzUOGhO5jJfAM6vuzisq3qqe_hJ1Dy-mpFQat4_ErfKRKRQOJvhKi4q"}

这是我的 GcmListenerService:

public class MyGcmListenerService extends GcmListenerService {

    private static final String TAG = "MyGcmListenerService";

    @Override
    public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
        Log.d(TAG, "From: " + from);
        Log.d(TAG, "Message: " + message);

        sendNotification(message);
    }

    private void sendNotification(String message) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        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_app_icon)
                .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());
    }
}

这是清单文件:

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

    <permission android:name="com.example.demoapp.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.example.demoapp.permission.C2D_MESSAGE" />

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <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" />
                <category android:name="com.example.demoapp" />
            </intent-filter>
        </receiver>
        <service
            android:name=".services.MyGcmListenerService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service
            android:name=".services.MyInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>
        <service android:name=".services.RegistrationIntentService"/>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

【问题讨论】:

  • 你可以试试 。它可能会解决问题。
  • 无法解决。

标签: android gcmlistenerservice


【解决方案1】:

在您的 Android Manifest 中,接收者的 &lt;intent-filter&gt; 标签中的类别名称 &lt;category android:name="com.example.gcm" /&gt; 必须与您的包名称相同。因此,将 com.example.gcm 替换为您在清单中声明的​​包名称。

这将允许您的接收器在应用未运行时接收 gcm 消息。

【讨论】:

  • 嗯...这很奇怪。除了包名称之外,您的配置看起来还不错。我建议您更新您的问题以反映正确的包名称,以免其他人指出相同的问题。
  • 我改成了我的包名,也一样,每当我从我最近的应用程序列表中滑动它时,GCM 不再工作。这是非常有趣的行为。
【解决方案2】:

确保您的有效负载在“数据”旁边也有“通知”。当应用程序处于后台时,消息将被发送到消息托盘。

类似这个例子

{
    "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
    "notification" : {
      "body" : "great match!",
      "title" : "Portugal vs. Denmark",
      "icon" : "myicon"
    },
    "data" : {
      "Nick" : "Mario",
      "Room" : "PortugalVSDenmark"
    }
  }

请参阅此处了解更多信息:https://developers.google.com/cloud-messaging/concept-options

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 2015-08-13
    相关资源
    最近更新 更多