应用程序在棉花糖关闭时未显示推送通知

时间:2016-08-03 19:16:11

标签: android push-notification android-6.0-marshmallow

当应用程序在marshmallow中关闭时,未收到推送通知。当我的应用程序打开其工作正常时。在棒棒糖工作中,罚款对于app打开或关闭无关紧要。

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

我在清单文件中给予许可。

1 个答案:

答案 0 :(得分:0)

除了这些权限之外,您还需要在AndroidManifest.xml文件中使用正确的intent过滤器指定BroadcastReceiver:

    <receiver
        android:name=".GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.example.yourapp" />
        </intent-filter>
    </receiver>

然后在你的BroadcastReceiver中显示一条通知:

public class GcmBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
        String messageType = gcm.getMessageType(intent);

        if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
             // Show Notification
        }
        setResultCode(Activity.RESULT_OK);
    }
}