1

我一直在我的應用程序之一中實施推送通知,並且它正常工作,直到時間應用程序打開或它在後臺運行。當應用程序關閉時無法接收通知

但是,從後臺刪除應用程序後,我無法收到我的設備上的通知。我經歷了幾乎所有與此問題相關的鏈接,並且很可能已經實現了所有需要收到通知的內容。

我只是停留在這一點上,所以如果有人知道如何在應用程序關閉時收到通知,請發佈解決方案。

我們將非常感謝您的幫助。

服務類代碼:

public void onMessageReceived(String from, Bundle data){ 
    String message = data.getString("message"); 
    //createNotification(mTitle, push_msg); 

    Log.e("*Notification-Response*" , from); 
    Log.e("*Notification-Bundle*" , String.valueOf(data)); 

    //checkApp(); 
    generateNotification(getApplicationContext(),message); 

} 


private static void generateNotification(Context context, String message) { 



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

    Intent notificationIntent = new Intent(context, Dashboard.class); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), notificationIntent, 0); 

    Notification notification = new NotificationCompat.Builder(context) 
      .setSmallIcon(R.drawable.appicon) 
      .setContentTitle("Title") 
      .setContentText(message) 
      .setContentIntent(pIntent) 
      .setAutoCancel(true) 
      .build(); 

    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    // Play default notification sound 
    notification.defaults |= Notification.DEFAULT_SOUND; 

    // Vibrate if vibrate is enabled 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    notificationManager.notify(999999, notification); 

} 

清單:

<service 
      android:name=".GCM.PushNotificationService" 
      android:exported="false"> 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      </intent-filter> 
     </service> 



     <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" /> 
       <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
       <category android:name="com.google.android.gcm.demo" /> 
      </intent-filter> 
     </receiver> 

感謝。

+0

[Android GCM(推送通知):設備在應用程序停止時沒有收到通知]的可能重複(http://stackoverflow.com/questions/12073449/android-gcm-push-notification-device-doesnt-接收通知,如果應用) – Exaqt

+0

發佈相關代碼 – saeed

+0

在發佈它作爲副本之前,只是想知道問題是什麼。我已經通過這個鏈接,但仍然沒有解決我的問題。 – shubham0703

回答

0

解決了這個問題。

這是設備相關的問題。在其他設備上測試過,它工作正常。

+0

你能解決這個問題嗎?我也有同樣的問題。在某些設備上推送通知不會在應用程序關閉時發生, –

+0

這是設備相關問題 – shubham0703

+0

您是否能夠解決該設備上的問題?我也有一個設備,即使應用程序關閉,whatsApp仍在工作。但是我的應用在關閉時沒有收到通知。在其他設備上工作正常。 –

0

可能是問題是註冊deviceID不正確。 Ondestroy()可能正在調用。因此,deviceID在從後臺移除應用時也會被移除。所以需要重新註冊deviceID。

+0

這是一個設備問題。上面的書面代碼工作正常。 – shubham0703

相關問題