[英]How to get Heads Up notification in android using Firebase Cloud Messaging(Notification) while the app is not in foreground?
I have an app that should show headsup notification while the app is in both foreground and background(not in history). 我有一个应用程序同时在前景和背景(不在历史记录中)时应显示提示通知的应用程序。
In foreground case , i acheived this by the folowing method. 在前台情况下,我通过以下方法实现了这一点。
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
builder.setFullScreenIntent(pendingIntent,true);
But in background, it always showing in the notification area.Can anybody tell how to acheive this in background(not in history) 但是在后台,它总是显示在通知区域中。任何人都可以告诉您如何在后台实现此功能(不在历史记录中)
i tried the below parameters for notification but not working. 我尝试了以下参数进行通知,但无法正常工作。
{
"to" : "_Token_",
"priority":"high",
"data":
{
"title": "Test",
"text": "Fcm"
},
"notification":
{
"title": "Test",
"text": "Fcm"
}
}
Firebase allows notification customization when data messages are used. 当使用数据消息时,Firebase允许自定义通知。 Data messages invokes onMessageReceived() method even when the app in backgroud so you create your custom notification.
即使应用程序在backgroud中,数据消息也会调用onMessageReceived()方法,因此您可以创建自定义通知。
You can take reference of this link to know more about data notifications Firebase notifications 您可以参考此链接以了解有关数据通知的更多信息Firebase通知
You should completely delete notification part from your Json and just use data ! 您应该从Json中完全删除通知部分,只使用数据! That's the trick.
那是诀窍。 I mean this:
我的意思是:
{
"to" : "_Token_",
"priority":"high",
"data":
{
"title": "Test",
"text": "Fcm"
}
}
When you have notification tag in your Json, sometimes the library decide to handle the notifications itself. 当您在Json中有通知标签时,有时库会决定自行处理通知。 so when your app is in foreground it wont call your on message receiver and it show the notification itself.
因此,当您的应用程序处于前台时,它不会调用您的on消息接收器,而是显示通知本身。
Just remove the notification and always use data tag. 只需删除通知,并始终使用数据标签即可。
It works both when your app is in foreground/background or killed and stopped 当您的应用程序处于前台/后台或已终止并停止运行时,它都可以工作
instead of using setFullScreenIntent(), try this: 而不是使用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);
Edit: for background you should do simillar. 编辑:对于背景,你应该做类似。 In order to show Heads-up notification there should be combination of high priority + sound and/or vibration (preferably both).
为了显示抬头通知,应将高优先级+声音和/或振动(最好同时使用)组合在一起。
Edit2: Preferably you can show this notification in onMessageReceived() method, here: https://firebase.google.com/docs/cloud-messaging/android/receive#override-onmessagereceived Edit2:最好您可以在以下位置的onMessageReceived()方法中显示此通知: https : //firebase.google.com/docs/cloud-messaging/android/receive#override-onmessagereceived
{
"to":"push-token",
"priority": "high",
"notification": {
"title": "Test",
"body": "Mary sent you a message!",
"sound": "default",
"icon": "youriconname"
}
}
Check this out: Create Heads-Up Display 检查一下: 创建平视显示器
You need to add your listener service, as you would in a standard GCM implementation. 您需要像在标准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]
Then, register your receiver in AndroidManifest.xml tag to listen on incoming notifications: 然后,在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] -->
This way - you won't have to handle incoming messages separately for cases when app is in foreground vs background. 这样-您无需在应用程序处于前台与后台情况下分别处理传入消息。
https://developers.google.com/cloud-messaging/ https://developers.google.com/cloud-messaging/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.