应用关闭时收到通知

Posted

技术标签:

【中文标题】应用关闭时收到通知【英文标题】:Receive notification when app closed 【发布时间】:2017-03-09 11:23:41 【问题描述】:

我使用 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" />

【问题讨论】:

请检查此答案类似于您的问题How to get android notifications when app was closed? 【参考方案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>

【讨论】:

【参考方案2】:

添加

        <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

【讨论】:

以上是关于应用关闭时收到通知的主要内容,如果未能解决你的问题,请参考以下文章

应用关闭时未收到 Android 推送通知

Sinch - 即时消息 - 应用关闭时收到推送通知

关闭应用程序时,Android 不会收到来自 Parse Push 的推送通知

应用关闭或被杀时未收到 Android FCM 通知

即使App已关闭,也会收到Android推送通知

应用完全关闭时获取 FCM 远程通知 iOS