我有一个应用程序,该应用程序应在该应用在前景和背景(不是历史记录)时显示headsup Notification。
在前景情况下,我通过效率方法实现了这一点。
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"
}
}
您应该从JSON完全删除通知部分,然后使用数据!那就是诀窍。我的意思是:
{
"to" : "_Token_",
"priority":"high",
"data":
{
"title": "Test",
"text": "Fcm"
}
}
当您在JSON中有通知标签时,有时库决定处理通知本身。因此,当您的应用在前景时,它不会致电您的消息接收器,并显示通知本身。
只需删除通知并始终使用数据标签。
当您的应用在前景/背景或杀死并停止
firebase允许使用数据消息时的通知自定义。即使在Backgroud中的应用程序中,数据消息也会调用OnMessageCeived()方法,因此您可以创建自定义通知。
您可以参考此链接以了解有关数据通知的更多信息firebase通知
而不是使用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()方法中显示此通知
{
"to":"push-token",
"priority": "high",
"notification": {
"title": "Test",
"body": "Mary sent you a message!",
"sound": "default",
"icon": "youriconname"
}
}
查看此内容:创建头部显示
您需要添加侦听器服务,就像在标准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标签中注册您的接收器以在传入通知上聆听:
<!-- [START gcm_listener] -->
<service
android:name="gcm.play.android.samples.com.gcmquickstart.MyGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<!-- [END gcm_listener] -->
这样 - 当应用在前景与背景时,您无需单独处理传入消息。
https://developers.google.com/cloud-messaging/