2017-07-11 56 views
-2

我已经实施了Firebase,但不幸的是,通知只在应用处于前台或后台时工作,但无法在应用关闭时收到任何通知。我尝试在网上冲浪,但无法获得任何结果。 即使在关闭它后,有什么办法让应用程序保持活跃状态​​? 如果是,我认为这将有助于收到通知。任何有用的建议都受到欢迎。谢谢FCM推送通知在前台和后台工作,但在应用关闭时无法工作

+0

显示代码是如何实现的。 – james

回答

0

我做的一件事是我不依赖通知响应,而是传递数据对象并自己做出自定义通知。

下面是代码,可以帮助你,因为我们可以访问数据,即使对象时,应用程序是开放及关闭的:

Map<String, String> dataMap = remoteMessage.getData(); 
String notif = dataMap.get("title"); 

然后我使用此功能进行通知

private void notificationManager(Context context, String message) { 

    try { 
     long when = System.currentTimeMillis(); 

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

     Log.v("message", "," + message); 

     Intent notificationIntent = new Intent(context, SplashActivity.class); 


     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
     builder.setAutoCancel(true); 
     builder.setContentTitle(context.getString(R.string.app_name)); 
     builder.setStyle(new NotificationCompat.BigTextStyle().bigText(message)); 
     builder.setContentText(message); 
     builder.setTicker(message); 
     builder.setLights(Color.GREEN, 500, 500); 
     builder.setWhen(when); 
     builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); 
     builder.setSmallIcon(R.mipmap.ic_launcher); 
     builder.setContentIntent(intent); 


     Notification notification = builder.build(); 
     notification.ledARGB = 0xFFff0000; 
     notification.flags = Notification.FLAG_SHOW_LIGHTS; 
     notification.ledOnMS = 100; 
     notification.ledOffMS = 100; 
     notificationManager.notify(1, notification); 

     PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
     PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG"); 
     wl.acquire(15000); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

希望这有助于。

0

FCM有可能你正在使用的消息显示两个消息服务

  1. 显示消息
  2. 数据消息

。您应该使用数据消息而不是显示消息。当应用关闭时,显示消息将无法使用。

相关问题