【问题标题】:Firebase(FCM) notifications not coming when android app removed from recent tray从最近的托盘中删除 android 应用程序时,Firebase(FCM)通知不会出现
【发布时间】:2016-11-02 21:15:44
【问题描述】:

我正在通过我自己的 web 服务使用 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.当应用程序在前台和后台没有任何问题时,通知将会到来,但是如果我从最近的托盘中删除了应用程序然后通知不来,我必须为此处理任何解决方案吗? (就像 Whatsup 通知显示所有场景,即使我从设置中强制停止此应用)

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

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());
    }
} 

失败的设备:(从最近的托盘中删除后,通知未发送)

  • 小米红米 Note 2 (5.0.2 Lollipop)
  • 华为荣耀5c(5.1.1 Lollipop)

成功的设备:(从最近的托盘中删除后会发出通知)

  • HTC One X (4.2 果冻豆)
  • 三星 Galaxy Grand Prime (4.4 Kitkat)

【问题讨论】:

  • 我认为这不是操作系统版本的问题(Lollipop vs Jelly Bean 等),更可能是是否允许 Google Play 服务唤醒已被recients解雇。一些制造商调整了这种行为。我建议向 firebase 支持提出问题,以便 FCM 团队可以更深入地调查未按预期运行的设备。 firebase.google.com/support
  • 我的数据消息也有同样的问题。据我所知,它不适用于任何版本的 Android。我已经在 Marshmallow 和 Lollipop 上进行了测试。
  • 熔岩爱尔兰 x1 也

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


【解决方案1】:

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

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

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

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

活动

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 中没有像“Send to ALL”这样的东西,唯一的方法是使用 Firebase 控制台,它有自己的问题要处理,比如白色图标问题,并撕掉通知的自定义参数。

【讨论】:

  • 关于问题3:我认为如果您将每个用户注册到“主题”,则可以“全部发送”,不是吗?!
  • @Hisham 我刚刚用经过测试的设备编辑了我的帖子,在我的情况下,棒棒糖前的设备工作正常,这意味着从最近的托盘中删除应用程序后会发出通知,但在 Lollipop 设备通知不是删除应用程序时来,对此有任何线索吗?还编辑了sendNotification(remoteMessage.getData().get("message"));
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

    这个问题需要后端实现。 让我讨论一下我是如何克服这个缺点的。

    我们了解 Activity 生命周期。所以在销毁时,我从后端调用一个服务,取消注册我的客户端令牌。 由于目标客户端的令牌未注册,所有传入消息现在都将排队等待传递。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 2017-12-04
      • 2017-04-08
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      • 2017-07-30
      相关资源
      最近更新 更多