我正在努力处理Android上的FCM通知。问题是只有当应用程序关闭而不运行的时候。我试图实现没有点击活动,我不希望应用程序打开,我不希望消息消失。当应用程序打开并运行时,它运行得非常完美。当应用程序没有打开时,我仍然会收到通知,但它不是多行的,所以我只能看到通知的开头。然后单击通知会使其消失。我花了好几个小时试图找到一个可行的解决方案。下面是我到目前为止掌握的代码:
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上是完美的。我只是不能得到信息显示正确的多行,我不知道该做什么。
*
发布于 2016-09-05 12:25:25
FCM在应用程序关闭时以它想要的方式显示我的通知。对我来说没问题。我只想确保整个通知被读取而不是丢失。这是我的解决方案。当应用程序用户单击通知时,它会打开应用程序,并在警报对话框中显示通知。
发送消息的c#服务器端代码
var msg2send = new
{
to = deviceId,
priority = 10,
notification = new
{
body = msg,
title = "Red Hill Car Wash",
icon = "myicon"
},
data = new
{
notice = msg
}
};在onCreate of MainActivity
String notice = "";
try {
notice = getIntent().getStringExtra("notice");
if (notice != null)
{
Log.i("myStuff", notice);
///// DISPLAY NOTIFICATION IN AN ALERT DIAGLOG all good!
}
}
catch (Exception ex) {
Log.i("myStuff", ex.getMessage());
}FCM将消息传递给我的MainActivity,如果消息存在,我就会抓取它,并以我想要的方式显示它。感谢您的意见和意见,以帮助解决这个问题。
发布于 2016-09-04 15:51:49
当您单击它时,通知将消失,因为在执行onClick时没有添加任何操作。
要在通知中提供onClick功能,您必须在目标活动中使用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);发布于 2016-09-05 12:37:36
一种方法是使用data而不是notification来获取应用前景或背景中的onMessageReceived()回调。json部分是:
{
"data": {
"title": "notification_title",
"body": "notification_body"
},
"to" : "jh578_gsh....jhHGFJ76FH"
}有关详细信息,请遵循以下链接:https://stackoverflow.com/a/37845174/5829624
编辑:
方法2:
在通知有效负载中使用click_action有效负载。在json中:
{
"notification": {
"click_action": "OPEN_ACTIVITY_1",
},
}并将此意图筛选器添加到清单中的活动中(当您点击通知时要加载它):
<intent-filter>
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>希望这个能解决你的问题。
https://stackoverflow.com/questions/39318275
复制相似问题