2017-03-09 129 views
0

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

請檢查這個答案類似於您的問題[?當應用程序被關閉如何獲得Android通知] (http://stackoverflow.com/questions/13741875/how-to-get-android-notifications-when-app-was-closed) – Shahrukh

回答

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

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> 
相關問題