2016-03-19 55 views
2

我需要在我的應用程序中創建並顯示一些自定義推送通知(需要傳遞一些數據並在點擊時打開一些特定的活動)。我做了所有類似於here的描述。爲什麼我的GcmListenerService.onMessageReceived在應用程序未運行時未被調用?

當應用程序運行時,一切都按預期工作。但是當它在後臺時,我的服務沒有啓動,並且我收到推送通知,它會將我帶到應用程序的啓動畫面。

我研究了很多,但沒有找到這種行爲的解釋。 任何人都可以告訴我爲什麼我的服務在應用程序在後臺時被忽略?

這是我的清單文件:

<permission 
    android:name="com.locdel.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" /> 

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

...................... 

<application 
    android:name=".LocdelApplication" 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 

    <receiver 
     android:name="com.google.android.gms.gcm.GcmReceiver" 
     android:exported="true" 
     android:permission="com.google.android.c2dm.permission.SEND"> 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <category android:name="com.locdel" /> 
     </intent-filter> 
    </receiver> 

    <service 
     android:name="com.locdel.gcm.LocdelGcmListenerService" 
     android:exported="false"> 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
     </intent-filter> 
    </service> 
    <service 
     android:name="com.locdel.gcm.LocdelIDListenerService" 
     android:exported="false"> 
     <intent-filter> 
      <action android:name="com.google.android.gms.iid.InstanceID" /> 
     </intent-filter> 
    </service> 
    <service 
     android:name=".gcm.RegistrationIntentService" 
     android:exported="false" > 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
     </intent-filter> 
    </service> 

....................... 

我甚至試圖擴大自己清醒的廣播接收器,並明確啓動服務。這是自定義的接收機的onReceive方法,它實際上是所謂的,但服務沒有再次啓動:

@Override 
public void onReceive(Context context, Intent intent) { 
    Intent serviceIntent = new Intent(context, LocdelGcmListenerService.class); 
    serviceIntent.setAction("com.google.android.c2dm.intent.RECEIVE"); 
    serviceIntent.putExtras(intent.getExtras()); 
    startWakefulService(context, serviceIntent); 
    setResultCode(Activity.RESULT_OK); 
} 
+0

嗨,你能說明你是如何在你的manifest.xml和服務的代碼中聲明服務的嗎? –

+0

更新了我的問題。代碼服務實際上並不重要。我已經覆蓋onMessageReceived方法並在那裏放置一些日誌。所以我知道服務根本沒有啓動 – shift66

+0

你在清單文件中提到的權限是什麼? – AndiM

回答

0

你需要檢查活動是否在運行時不喜歡這種方式獲得

ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); 
    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1); 
    ComponentName componentInfo = taskInfo.get(0).topActivity; 
    if(componentInfo.getPackageName().equalsIgnoreCase("com.example.myapp")){ 

     updateMyActivity(message); //send broadcast to notify app 



     //Activity Running 
//   Send a broadcast with the intent-filter which you register in your activity 
//   where you want to have the updates 
     } 
     else{ 
      //Activity Not Running 
      //Generate Notification 
      sendNotification(message); 
     } 
+0

哪裏?問題是沒有收到消息。 – shift66

+0

@ shift66我有點不清楚是在應用程序處於後臺還是不在時調用onReceived? –

+0

是的,onReceive的BroadcastReceiver被調用,我甚至手動啓動那裏的服務,它不是由於某種原因啓動 – shift66

0

嘗試從的onReceive

private void sendNotification(String message) { 
      Intent intent = new Intent(this,FullMapActivity.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    //  int color = getResources().getColor(R.color.my_notif_color); 
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
      /*PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 *//* Request code *//*, intent, 
        PendingIntent.FLAG_ONE_SHOT);*/ 

      Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
      if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { 



       NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
         .setSmallIcon(R.drawable.headerlogo) 
         .setContentTitle("hey..! your booking is confirmed") 
         .setContentText(message) 
         .setAutoCancel(true) 
         .setSound(defaultSoundUri) 
         .setContentIntent(pendingIntent); 


       NotificationManager notificationManager = 
         (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

       notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
      } 
      else 
      { 

       NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
         .setSmallIcon(R.drawable.headerlogo) 
         .setContentTitle("hey..! your booking is confirmed") 
         .setContentText(message) 
         .setAutoCancel(true) 
    //     .setColor(color) 
         .setSound(defaultSoundUri) 
         .setContentIntent(pendingIntent); 


       NotificationManager notificationManager = 
         (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

       notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
      } 
     } 
0

https://github.com/google/gcm/issues/63

呼叫通知以這種方式了一封郵件上面的鏈接明確提到,如果「通知」載荷存在,那麼android會觸發通知。嘗試刪除「通知」有效載荷併發送「數據」有效載荷,應調用您的onMessageReceived

+0

如何處理iOS?看來iOS只處理「通知」有效載荷 – yfsx

+0

是的。您無法控制IOS中的通知。這是發起通知的服務器,通知只是顯示在手機中。服務器將不得不指定目標視圖和其他信息。應用程序可以肯定不會在處理它時處理它。它可以處理它,當它在後臺模式(雖然不確定)。這就是爲什麼Whatsapp爲收到的每封郵件發送不同通知的原因。在Android中,它會積累它們並將它們顯示爲一個。希望這可以幫助 –

相關問題