【问题标题】:How to auto launch android app when you receive notification [FCM]?收到通知 [FCM] 时如何自动启动 Android 应用程序?
【发布时间】:2017-03-23 12:32:11
【问题描述】:

我想在用户收到应用通知时自动启动应用,启动器图标上不会有点击操作。

【问题讨论】:

标签: android


【解决方案1】:

在您的onMessageReceived() 方法中,您可以尝试添加您的startActivity(intent) 代码。这样,当应用程序收到 FCM 消息时,它会启动应用程序。像这样……

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        startActivity(new Intent(this, MainActivity.class));
    }
}

【讨论】:

  • 从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志。收到此错误
  • onMessageReceived 在应用程序处于后台时不会调用,因此无论如何这对我没有帮助
  • 感谢您的回复,即使您的帖子也是我解决方案的一部分:)
  • 有什么解决办法吗?谢谢
【解决方案2】:

如果通知正文不包含“通知”参数,当应用程序在后台运行时,方法 onMessageReceived() 可以正常工作。所有数据都应粘贴在“数据”中。像这样:

{
    "to":"token",
    "priority":"high",
    "data": {
        "title": "Carmen",
        "text": "Популярные новости за сегодня!",
        etc..
    }
}

然后您可以在代码中解析它并在通知中显示标题和文本。

例如:

override fun onMessageReceived(remoteMessage: RemoteMessage?) {
        Log.d("FirebaseService", "Receive notification: ${remoteMessage?.data}")
        remoteMessage?.let { showNotification(remoteMessage) }
    }

    private fun showNotification(remoteMessage: RemoteMessage) {
        val notificationModel: NotificationModel
                = getNotificationModelFromMessageData(remoteMessage.data)

        val intent = Intent(this, SplashActivity::class.java)
        intent.putExtra(NOTIFICATION_ARGUMENT, notificationModel)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

        val pendingIntent = PendingIntent.getActivity(
                this, NOTIFICATION_RECEIVE_REQUEST_CODE,
                intent, PendingIntent.FLAG_ONE_SHOT)

        val defaultNotificationSound: Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        val notification: Notification
                = NotificationCompat.Builder(this)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setContentTitle(notificationModel.title)
                .setContentText(notificationModel.text)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setSound(defaultNotificationSound)
                .build()

        val notificationManager: NotificationManager
                = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.notify(NOTIFICATION_ID, notification)
    }

    private fun getNotificationModelFromMessageData(jsonData: MutableMap<String, String>): NotificationModel {
        return NotificationModel(
                jsonData[TITLE_PARAMETER] as String,
                jsonData[TEXT_PARAMETER] as String,
                jsonData[DAYS_PARAMETER] as String,
                jsonData[MESSAGE_ID] as String)
    }

希望对您有所帮助!

【讨论】:

  • 感谢您的回复我正在从 FCM 获取数据,例如 onMessageReceived 中的 {Key=value} 如何实现您显示的 JSON 格式..
  • 最后我解决了我的问题谢谢这是因为我没有使用任何 REST 客户端所以数据格式是 {key = value} 这在前台应用程序时基本上可以工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-20
  • 1970-01-01
相关资源
最近更新 更多