我有一个应用程序应该显示抬头通知,而应用程序在前台和后台(不在历史记录中)。
在前景的情况下,我通过以下方法实现了这一点。
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
builder.setFullScreenIntent(pendingIntent,true);
但在后台,它总是显示在通知区域。任何人都可以告诉如何在背景中实现这一点(不是在历史中)
我尝试使用以下参数进行通知但无法正常工作。
{
"to" : "_Token_",
"priority":"high",
"data":
{
"title": "Test",
"text": "Fcm"
},
"notification":
{
"title": "Test",
"text": "Fcm"
}
}
答案 0 :(得分:2)
Firebase允许在使用数据消息时进行通知自定义。即使在backgroud中的应用程序中,数据消息也会调用onMessageReceived()方法,因此您可以创建自定义通知。
您可以参考此链接以了解有关数据通知的更多信息 Firebase notifications
答案 1 :(得分:2)
您应该从Json中完全删除通知部分,只使用数据!这就是诀窍。 我的意思是:
{
"to" : "_Token_",
"priority":"high",
"data":
{
"title": "Test",
"text": "Fcm"
}
}
当您的Json中有通知标记时,库有时会决定自己处理通知。因此,当您的应用程序位于前台时,它不会调用您的消息接收器并显示通知本身。
只需删除通知并始终使用数据标记。
当您的应用处于前台/后台或被杀死并停止
时,它都有效答案 2 :(得分:0)
而不是使用setFullScreenIntent(),试试这个:
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
builder.setPriority(NotificationCompat.PRIORITY_MAX);
builder.setSound(URI_TO_SOUND);
builder.setVibrate(ARRAY_WITH_VIBRATION_TIME);
编辑:对于背景你应该做simillar。为了显示单挑通知,应该有高优先级+声音和/或振动(最好是两者)的组合。
Edit2:您最好在onMessageReceived()方法中显示此通知,此处:https://firebase.google.com/docs/cloud-messaging/android/receive#override-onmessagereceived
答案 3 :(得分:0)
{
"to":"push-token",
"priority": "high",
"notification": {
"title": "Test",
"body": "Mary sent you a message!",
"sound": "default",
"icon": "youriconname"
}
}
答案 4 :(得分:0)
您需要添加侦听器服务,就像在标准GCM实现中一样。
public class MyGcmListenerService extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
/**
* Called when message is received.
*
* @param from SenderID of the sender.
* @param data Data bundle containing message data as key/value pairs.
* For Set of keys use data.keySet().
*/
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
if (from.startsWith("/topics/")) {
// message received from some topic.
} else {
// normal downstream message.
}
// [START_EXCLUDE]
/**
* Production applications would usually process the message here.
* Eg: - Syncing with server.
* - Store message in local database.
* - Update UI.
*/
/**
* In some cases it may be useful to show a notification indicating to the user
* that a message was received.
*/
sendNotification(message);
// [END_EXCLUDE]
}
// [END receive_message]
然后,在AndroidManifest.xml标记中注册您的接收器以侦听传入的通知:
public class MyGcmListenerService extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
/**
* Called when message is received.
*
* @param from SenderID of the sender.
* @param data Data bundle containing message data as key/value pairs.
* For Set of keys use data.keySet().
*/
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
if (from.startsWith("/topics/")) {
// message received from some topic.
} else {
// normal downstream message.
}
// [START_EXCLUDE]
/**
* Production applications would usually process the message here.
* Eg: - Syncing with server.
* - Store message in local database.
* - Update UI.
*/
/**
* In some cases it may be useful to show a notification indicating to the user
* that a message was received.
*/
sendNotification(message);
// [END_EXCLUDE]
}
// [END receive_message]
这种方式 - 对于应用程序处于前台与后台的情况,您不必单独处理传入的消息。