gpt4 book ai didi

Android FCM 通知问题

转载 作者:行者123 更新时间:2023-11-29 17:15:18 33 4
gpt4 key购买 nike

我正在努力处理 Android 上的 FCM 通知。问题仅在应用程序关闭且未运行时出现。我正在尝试实现无点击 Activity 我不希望应用程序打开并且我不希望消息消失。当应用程序打开并运行时,它可以完美运行。当应用程序未打开时,我仍然会收到通知,但它不是多行的,所以我只能看到通知的开头。然后单击通知使其消失。我花了几个小时试图找到一个可行的解决方案。这是我到目前为止的代码:

private void sendNotification(String messageBody) {

//Intent intent = new Intent(this, MainActivity.class);
// intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
//PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
// PendingIntent.FLAG_UPDATE_CURRENT);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My Car Wash App")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentText(messageBody)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setPriority(NotificationCompat.PRIORITY_MAX);

NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}

如有任何帮助或建议,我们将不胜感激。

编辑 *******

问题是经过更多研究后我问的问题很糟糕我现在意识到上面的代码只在我的应用程序打开时被调用。

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
//It is optional
Log.i("myStuff", "From: " + remoteMessage.getFrom());
Log.i("myStuff", "Notification Message Body: " + remoteMessage.getNotification().getBody());
//Calling method to generate notification
sendNotification(remoteMessage.getNotification().getBody());
}

当应用程序不在前台时,不会调用 onMessageReceived,这就是除非应用程序正在运行,否则通知不会显示多行的原因。有很多关于这个主题的信息发布,但我似乎仍然无法弄清楚。

这是我的 C# 服务器端代码 *

var data = new
{
to = deviceId,
notification = new
{
body = msg,
title = "My Car Wash",
icon = "myicon"
},
priority = 10
};

当应用程序运行时,通知在 iOS 上完美运行,在 Android 上完美运行。我只是无法让消息正确显示在多行中,我不确定该怎么做。

*

最佳答案

当您点击它时,通知会消失,因为您在执行 onClick 时没有添加任何操作。

要在通知上提供 onClick 功能,您必须对目标 Activity 使用 PendingIntent。如果需要,您还可以向其中添加其他数据。

在初始化notificationManager之前,添加这段代码:

Intent openIntent = new Intent(mContext, TargetActivity.class);
// Falg to clear the stack.
openIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,
openIntent, 0);
notificationBuilder.setContentIntent(pendingIntent);

关于Android FCM 通知问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39318275/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com