【问题标题】:can't receive message gcm android below lollipop棒棒糖下面无法接收消息gcm android
【发布时间】:2016-11-05 17:00:05
【问题描述】:

我无法在 lollipop 下运行 android 的设备上接收 GCM 消息。 推送消息适用于 android 5、6 和 7。 我真的不知道为什么......有我的应用程序代码:

清单:

<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="25"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />

<permission android:name="sbntt.android.xenoss.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="sbntt.android.xenoss.permission.C2D_MESSAGE" />

<application
    android:name="android.support.multidex.MultiDexApplication"
    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" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="sbntt.android.xenoss" />
        </intent-filter>
    </receiver>
    <service
        android:name="sbntt.android.xenoss.GCMPushReceiverService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
    <service
        android:name=".GCMRegistrationIntentService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.iid.InstanceID" />
        </intent-filter>
    </service>
    <service
        android:name=".GCMTokenRefreshListenerService"
        android:exported="false">
    </service>

</application>

GCMPushReceiverService 类

@Override
public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("message");
    if (Settings.preference != null) {
        if (Settings.preference.getBoolean("notifications", true)) {
            sendNotification(message);
        }
    } else {
        sendNotification(message);
    }
}


private void sendNotification(String message) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    int requestCode = 0;
    PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_menu_youtube)
            .setContentTitle("TITLE")
            .setContentText(message)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setSound(sound)
            .setDefaults(Notification.DEFAULT_ALL)
            .setPriority(Notification.PRIORITY_HIGH);

    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, noBuilder.build());
}

注册工作正常! 感谢您的帮助。

【问题讨论】:

    标签: android google-cloud-messaging


    【解决方案1】:

    GCM 需要在设备上更新播放服务。您可以通知用户更新 Play Service 或在您的项目中使用较少的 Play Service 版本。

    【讨论】:

    • 主要活动有播放服务检查。运行 kitkat 的设备具有最新的 google play services 版本
    • 您在 adb Log wit play service tag 中收到任何消息?
    • 我卸载了设备上的播放服务更新,然后我再次更新它并且它工作......感谢您的帮助
    猜你喜欢
    • 2015-04-01
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多