当前位置: 首页 > 知识库问答 >
问题:

如何在收到通知[FCM]时自动启动android应用程序?

公良泰宁
2023-03-14

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

共有2个答案

笪智志
2023-03-14

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

{
    "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)
    }

希望有帮助!

陆俭
2023-03-14

在< code>onMessageReceived()方法中,可以尝试添加< code > start activity(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));
    }
}
 类似资料:
  • 我正在处理FCM通知。当应用程序在后台时,我的通知工作正常,但当应用程序在前台时,我的通知无法接收。我几乎什么都试过了,但都不奏效。当应用程序处于前台时,不会收到通知。 manifest.xml: 代码(FirebaseMessagingService):

  • 手机是OnePlus3T。oxygen OS版本为4.1.6。当应用程序在前台、后台但在内存中时,应用程序会收到通知。但当应用程序不在内存中(即从内存中刷出)时不会收到通知。其他安装了android操作系统版本4.2、5.1.1、6.0.1、7.1.1的设备也会收到通知,即使应用程序不在内存中。 好心建议点什么。提前道谢。

  • 我正在发送纯FCM数据通知到一个Android客户端,它们是端到端加密的。android应用程序解密数据通知负载,并从加密的数据创建本地通知。只要应用程序在前台或后台运行,这就可以正常工作:通知负载会被接收、解密,甚至会显示在锁定屏幕上,但只有在应用程序运行的时候才可以。我还查看了消息优先级,并在通知消息的AndroidConfig成员和数据字段中将其设置为“high”(发送到FCM的应用服务器)

  • 问题内容: 我尝试使用范围类型APPLICATION和带有@ Create,@ Beg的方法来注释类,但这似乎不起作用。 我想要的是在应用程序启动时立即启动无限循环。 问题答案: 如果希望在初始化后立即执行方法,则可以使用以下注释:

  • 手动设置“自动取消”标志 改用builder.build() notificationmanger取消(id)和/或取消所有 使用NotificationCompat.Builder 对setContentIntent使用常规启动意图,而不是手动调用StartActivity

  • 按照这个指南,我将一个Android应用程序连接到了Google Firebase云消息服务(FCM ), 我按照这个答案建立了FCM之间的连接 我可以成功接收从FCM控制台发送的消息,但不能从AWS SNS控制台接收。 AWS上记录的消息传递状态显示我发送的每一条消息都成功,而我的设备上没有显示任何通知。 有没有办法检查发生了什么?