我正在努力处理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-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);https://stackoverflow.com/questions/39318275
复制相似问题