应用关闭时收到通知

时间:2017-03-09 11:23:41

标签: android push-notification notifications onesignal

我用onesignal向我的应用发送通知。当我发送通知及其打开时,该应用程序工作完美。不幸的是,即使我设置广播接收器,应用程序也不会收到通知。

<receiver
        android:name="com.onesignal.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="${applicationId}" />
        </intent-filter>
    </receiver>
 <receiver android:name="com.onesignal.BootUpReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>
<service android:name="com.onesignal.GcmIntentService" />
    <service
        android:name="com.onesignal.SyncService"
        android:stopWithTask="false" />

2 个答案:

答案 0 :(得分:1)

只需让接收器类扩展WakefulBroadcastReceiver并在清单中添加以下行:

<receiver android:name=".YOUR_RECEIVER_CLASS"
       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="com.example.gcm"/>
</intent-filter>
</receiver>

答案 1 :(得分:0)

添加

        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />

到GcmBroadcastReceiver

并且

<receiver
        android:name="com.onesignal.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="${applicationId}" />
        </intent-filter>
    </receiver>

应该是

<receiver
        android:name="com.onesignal.GcmBroadcastReceiver"
    <permission android:name="<your app package>.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="<your app package>.permission.C2D_MESSAGE" />
</receiver>

取决于GCM BroadcastReceiver fired only when app is running or running at background