当Android应用程序从最近的托盘中删除时,Firebase(FCM)通知不会出现

时间:2016-06-30 16:32:49

标签: android firebase notifications android-notifications firebase-cloud-messaging

我通过自己的网络服务使用PHP发送Firebase通知,如下所示。

$url = 'https://fcm.googleapis.com/fcm/send';
        $fields = array(
             'registration_ids' => $tokens,
             'data' => $message
            );
        $headers = array(
            'Authorization:key = SERVER_KEY ',
            'Content-Type: application/json'
            );

1.当应用程序处于Foreground和Background时没有任何问题,通知即将到来,但如果我从最近的托盘中删除了应用程序然后通知没有到来,我必须要处理这个问题吗? (就像Whatsup通知显示所有场景,即使我强行停止此应用程序设置)

2.我收到通知后,来自" onMessageReceived"如何将这些消息,正文内容传递给我的Activity / Fragments?

3.在某些情况下,我想向所有人发送通知,而不是将所有令牌添加到数组中。任何其他方式来处理像#"发送给所有"类似的东西?

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "MsgService";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Data Payload : " + remoteMessage.getData());
        sendNotification(remoteMessage.getData().get("message"));
    }
    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Firebase Push Notification")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    }
} 

设备失败:(从最近的托盘中删除后,通知不会

  • MI Redmi Note 2(5.0.2 Lollipop)
  • 华为荣誉5c(5.1.1 Lollipop)

成功设备:(从Recent Tray中移除后会发出通知)

  • HTC One X(4.2果冻豆)
  • 三星galaxy grand prime(4.4 Kitkat)

3 个答案:

答案 0 :(得分:5)

  

1.当App在前台和后台没有任何问题时,通知即将到来,但如果我从最近的托盘中删除了应用程序   通知没有到来,我必须处理这个任何解决方案   这个 ? (就像Whatsup通知显示所有场景甚至我   强制从设置中停止此应用程序)

根据您的代码,您必须在remoteMessage.getNotification().getBody()上获取空指针异常,因为您没有发送通知,而是从您的curl请求发送数据有效负载。根据我的个人经验,您刚才提到的问题尤其在Android Kitkat或以下设备中出现过。在棒棒糖或以上Firebase推动似乎工作正常。

  

2.一旦我收到通知,从“onMessageReceived”如何将这些消息,正文内容传递给我的Activity / Fragments?

您可以从onMessageReceived()发出广播,并在您的活动中注册Braodcast接收器,并以您的广播意图发送数据。示例代码:

活动

private BroadcastReceiver receiver;

@Overrride
public void onCreate(Bundle savedInstanceState){

  // your oncreate code

  IntentFilter filter = new IntentFilter();
  filter.addAction("SOME_ACTION");

  receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
      String message = intent.getStringExtra("message");
    }
  };
     registerReceiver(receiver, filter);
}

 @Override
 protected void onDestroy() {
  if (receiver != null) {
   unregisterReceiver(receiver);
   receiver = null
  }
  super.onDestroy();
 }

FirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "MsgService";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getData());

        Intent intent=new Intent();
        intent.setAction("SOME_ACTION");
        intent.putExtra("message", remoteMessage.getNotification().getBody());
        sendBroadcast(intent);
    }
} 
  

3.在某些情况下,我想向所有人发送通知,而不是将所有令牌添加到数组中。任何其他处理方式,如“发送到所有”   类似的东西?

Firebase API中没有“发送给所有人”,唯一的方法是使用Firebase控制台,它有自己的问题可以处理白色图标问题,并翻录通知的自定义参数。

答案 1 :(得分:1)

我在精确的Mi Phone和华为手机上遇到了FCM的问题!事实证明,这不是FCM或您的应用程序的问题,而是在他们的设备级别。

他们具有禁用通知甚至唤醒锁定的设备级应用设置,因此当消息到达时不会触发onMessageReceived()。我更改了设置后(主要是允许通知并禁用我的应用程序的电池优化),一切正常!

请参阅Firebase cloud messaging not received when app in background

答案 2 :(得分:-1)

此问题需要后端实施。 让我来讨论一下如何克服这个缺点。

我们知道活动生命周期。因此,在销毁时,我从后端调用一个服务来取消注册我的客户端令牌。 由于目标客户端的令牌未注册,因此所有传入消息现在将排队等待传递。

因此,在恢复应用程序时,我会调用一个注册我的客户端的后端服务。 后端现在开始使用我的设备的新客户端令牌从队列发送消息。