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

pushnotification not showing when app close in marshmallow

在 marshmallow 中关闭应用程序时未收到推送通知。当我的应用程序打开时它工作正常。在棒棒糖中,应用程序打开或关闭都可以正常工作。

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

我在清单文件中授予权限。

除了这些权限之外,您还需要在 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);
    }
}