kotlin Android通知click_action无意义的词

wnavrhmk  于 2023-10-23  发布在  Kotlin
关注(0)|答案(2)|浏览(119)
{
    "to": "FCM token",

    "notification": {
        "title": "this is sample title",
        "body":"this is sample body",
        "click_action": "abc"
    },
    "data": {
        "sequence" : "84"

    } 
}

如果您在click_action值中输入任何单词并在设备上单击推送通知,则没有响应。
我应该把活动名称放在click_action中吗?

{
    "to": "FCM token",

    "notification": {
        "title": "this is sample title",
        "body":"this is sample body",
        "click_action": ""
    },
    "data": {
        "sequence" : "84"

    } 
}

在Android中,可以将click_action的值作为空值接收吗?

vlurs2pr

vlurs2pr1#

要正确处理通知点击,您应该将click_action字段设置为有意义的操作。此操作应对应于AndroidManifest.xml文件中定义的Activity或Intent过滤器。举例来说:

{
    "to": "FCM token",
    "notification": {
        "title": "This is a sample title",
        "body": "This is a sample body",
        "click_action": "com.example.myapp.MY_CUSTOM_ACTION"
    },
    "data": {
        "sequence" : "84"
    }
}

在此示例中,“com.example.myapp.MY_CUSTOM_ACTION”应与应用清单文件中指定的操作匹配。当用户点击通知时,Android将启动相应的Activity或执行指定的操作。
如果您希望在Android应用中将click_action作为空值接收,则可以将其设置为空字符串,如第二个JSON示例中所述。在这种情况下,当用户单击通知时,您的应用可以检查click_action是否为空,并相应地决定行为。
祝你好运

  • 添加-
    可以使用空值处理click_action。当用户点击通知时,您的应用可以通过覆盖FCM服务的onMessageReceived方法或使用通知点击处理程序来处理它。
"click_action": ""

设置点击答案为空,就像你做的那样。

And in your app, override the onMessageReceived method:

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // Handle the received FCM message here, including the notification data and optional click_action.

        // When click_action is empty, you can still handle the notification here.
        // For example, show it in the notification tray or open a specific activity.

        // You can access notification data like this:
        String title = remoteMessage.getNotification().getTitle();
        String body = remoteMessage.getNotification().getBody();
        
        // Handle the notification as needed.
    }
}

虽然click_action通常用于指定在单击通知时要打开的显式操作或Activity,但您仍然可以通过在onMessageReceived方法中处理通知数据来处理没有它的通知。

ryevplcw

ryevplcw2#

首先,你必须发送额外的参数,如click_action在数据对象中,而不是在通知中,因为它们有字符限制,取决于设备到设备
从Data对象中,你可以得到如下值:

val json = JSONObject(remoteMessage.data.toString())
   val data = json.getJSONObject("data")
   val click_action = data.getString("click_action ")

通过下面的代码,您可以选择特定的Activity并在该Activity中传递数据

var resultIntent = Intent(applicationContext, SplashActivity::class.java)
resultIntent.putExtra("title", title)
resultIntent.putExtra("click_action", click_action)

并将此Intent设置为PendingIntent(我在其他类中将其用作通知实用程序)

val icon = R.mipmap.ic_launcher
        intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
        val resultPendingIntent :PendingIntent ;
        if (Build.VERSION.SDK_INT >= 31) {
            resultPendingIntent = PendingIntent.getActivity(
                mContext,
                Random().nextInt(),
                intent,
                 PendingIntent.FLAG_IMMUTABLE
            )
        } else {
            resultPendingIntent = PendingIntent.getActivity(
                mContext,
                Random().nextInt(),
                intent,
                PendingIntent.FLAG_CANCEL_CURRENT
            )
        }

并将其设置为内容意图

val notificationBuilder = NotificationCompat.Builder(
            mContext, channelId
        )/*
        notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
            */
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentIntent(resultPendingIntent)
            .setSound(alarmSound)
            .setStyle(bigPictureStyle)
            .setWhen(getTimeMilliSec(timeStamp))
            .setSmallIcon(R.drawable.ic_stat_name)
            .setLargeIcon(BitmapFactory.decodeResource(mContext.resources, icon))
            .setContentText(message)

相关问题