【问题标题】:Going from foreground state to background state does not receive firebase notification从前台状态转到后台状态不会收到 firebase 通知
【发布时间】:2020-11-19 10:23:36
【问题描述】:

我将这个答案用于前台和后台以接收 firebase 通知 --> https://stackoverflow.com/a/38451582/12553303

实际上以下是我的疑问:---------

如果我没有为前台条件编写代码(谈论推送通知)我仍然会在我的应用程序处于后台时收到通知吗???-->是的

但是当我处于前台状态并且我从 firebase 推送通知时-->我不会在状态栏上看到通知也可以(因为假设我没有覆盖 onmessagereceive() 方法)....然后接下来我去背景状态我没有看到任何我发送的背景通知

我应该怎么做才能获得我发送给后台的通知,比如从前台状态(没有 onmessagereceived 的方法())到 背景状态??*

感谢需要对此的建议和澄清...

这甚至可能让通知从前台状态移动到 背景状态??

代码:----

override fun onMessageReceived(p0: RemoteMessage) {
    super.onMessageReceived(p0)
    Log.d("msg", "onMessageReceived: " + p0.getData().get("message"))
    val intent = Intent(this, OrderListActivity::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
    val channelId = "Default"
    val builder: NotificationCompat.Builder =NotificationCompat.Builder(this, channelId)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle(p0.getNotification()?.getTitle())
        .setContentText(p0.getNotification()?.getBody()).setAutoCancel(true)
        .setContentIntent(pendingIntent)
    val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = NotificationChannel(channelId,
            "Default channel",
            NotificationManager.IMPORTANCE_DEFAULT)
        manager.createNotificationChannel(channel)
    }
    manager.notify(0, builder.build())

}

任何建议将不胜感激,谢谢

【问题讨论】:

    标签: android firebase kotlin push-notification firebase-cloud-messaging


    【解决方案1】:

    为了在 Android 应用中接收消息,Firebase 通知在应用处于前台或后台时包含不同的机制。

    当应用关闭时,您的通知将由 Google 服务进程处理,该进程会根据需要显示您的通知,包括点击操作(即打开应用和通知图标)。

    当应用处于前台时,接收到的消息由应用处理。

    要解决此问题,请遵循以下步骤:-

    1.创建一个扩展 FirebaseMessagingService 的新类 2.实现onMessageReceived()方法 3.然后从remoteMessage中获取Notification对象并创建你自己的Android Notification,比如,

    public class MyNotificationService extends FirebaseMessagingService { 
      @Override
      public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Notification notification = new NotificationCompat.Builder(this)
          .setContentTitle(remoteMessage.getNotification().getTitle())
          .setContentText(remoteMessage.getNotification().getBody())
          .setSmallIcon(R.mipmap.ic_launcher)
          .build();
         NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
         manager.notify(1, notification);
       }
    }
    

    4.然后在你的 AndroidManifest.xml 中添加服务

    <service android:name=”.MyNotificationService”>
       <intent-filter>
          <action android:name=”com.google.firebase.MESSAGING_EVENT”/>
       </intent-filter>
    </service>
    

    【讨论】:

    • 如果我没有在我的代码中添加onmessageReceived 怎么办...假设我的应用程序在后台我会收到通知但是当我的应用程序在前台时通知没有来(那没关系,因为我没有覆盖那个方法)当我回到后台时,我没有收到任何通知....那个通知去哪儿了?它永远不会收到吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    • 2012-01-07
    • 2022-08-18
    • 2020-08-02
    • 1970-01-01
    相关资源
    最近更新 更多