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

相关问题