我正在Android中实施FCM通知,但通知如何根据应用状态(背景与前景)而有所不同?
我使用带有Postman的FCM API发送通知,这是通知结构:
{ "notification": {
"title": "Notification title",
"body": "Notification message",
"sound": "default",
"color": "#53c4bc",
"click_action": "MY_BOOK",
"icon": "ic_launcher"
},
"data": {
"main_picture": "URL_OF_THE_IMAGE"
},
"to" : "USER_FCM_TOKEN"
}
要渲染的图像取自data.main_picture
。
我已经实现了自己的FirebaseMessagingService
,这使得通知在前景状态下完美显示。通知代码是下一个:
NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
notiStyle.setSummaryText(messageBody);
notiStyle.bigPicture(picture);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(bigIcon)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setStyle(notiStyle); code here
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
但是,在后台,甚至没有执行该服务。在AndroidManifest.xml
中,Firebase服务声明如下:
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
我的问题不是LargeIcon
或SmallIcon
,而是显示大局。
感谢您的支持。
FCM notification messages
不支持largeIcon或bigPicture。
如果你在后台需要它们,你可以使用FCM data message
。
对于数据消息,始终调用onMessageReceived(message)
方法,因此您可以使用message.getData()
方法并创建自定义通知。
在此处阅读有关通知消息和数据消息的更多信息:qazxsw poi
例如,如果您要发送推送通知,则会获取数据中的所有必需内容而不是通知
@Override
public void zzm(Intent intent) {
Log.e(TAG, "zzm : " + intent);
createBigPictureNotification();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
}
删除通知对象并从数据对象获取所有值。
希望它对你有用。
8月2019年更新。
[因为py不支持通知的最新变更而浪费了几天]
只需将image = url添加到通知对象中即可。
它适用于原生Android。只需将{
"data": {
"main_picture": "URL_OF_THE_IMAGE",
"title": "Notification title",
"click_action": "MY_BOOK",
"color": "#53c4bc",
},
"to" : "USER_FCM_TOKEN"
}
添加到通知对象中即可。另请注意,在Python库中,图像字段不存在。 [截至8月19日] image
我使用PHP和这个库https://github.com/firebase/firebase-admin-python它的超级简单,更重要的是,当app在后台或被杀死时,它适用于大图像通知。
码:
https://github.com/kreait/firebase-php/
请参阅我的FirebaseMessagingService
https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FirebaseMessageService";
Bitmap bitmap;
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// There are two types of messages data messages and notification messages. Data messages are handled
// here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
// traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
// is in the foreground. When the app is in the background an automatically generated notification is displayed.
// When the user taps on the notification they are returned to the app. Messages containing both notification
// and data payloads are treated as notification messages. The Firebase console always sends notification
// messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
//
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
//The message which i send will have keys named [message, image, AnotherActivity] and corresponding values.
//You can change as per the requirement.
//message will contain the Push Message
String message = remoteMessage.getData().get("message");
//imageUri will contain URL of the image to be displayed with Notification
String imageUri = remoteMessage.getData().get("image");
//If the key AnotherActivity has value as True then when the user taps on notification, in the app AnotherActivity will be opened.
//If the key AnotherActivity has value as False then when the user taps on notification, in the app MainActivity will be opened.
String TrueOrFlase = remoteMessage.getData().get("AnotherActivity");
//To get a Bitmap image from the URL received
bitmap = getBitmapfromUrl(imageUri);
sendNotification(message, bitmap, TrueOrFlase);
}
/**
* Create and show a simple notification containing the received FCM message.
*/
private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("AnotherActivity", TrueOrFalse);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(image)/*Notification icon image*/
.setSmallIcon(R.drawable.firebase_icon)
.setContentTitle(messageBody)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image))/*Notification with Image*/
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
/*
*To get a Bitmap image from the URL received
* */
public Bitmap getBitmapfromUrl(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
return bitmap;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
如果您的问题与显示大图像有关,即您是使用来自firebase控制台的图像发送推送通知,并且仅当应用程序位于前台时才显示图像。此问题的解决方案是发送仅包含数据字段的推送消息。
REFERENCE HERE
这绝对解决了这个问题。
包含通知和数据有效负载的消息(如与Postman一起发送的示例)将由FCM库自动显示给最终用户设备。这不包括(大)图像。
我猜你有两种可能性:
{ "data": { "image": "https://static.pexels.com/photos/4825/red-love-romantic-flowers.jpg", "message": "Firebase Push Message Using API" "AnotherActivity": "True" }, "to" : "device id Or Device token" }
我更喜欢第二种选择。祝好运!
如果2019年有人登陆,你可以简单地在通知对象中添加一个图像字段:
{
"message": {
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"title" : "Awesome title",
"body" : "Your awesome push notification body",
"image" : "your_image_url"
}
}
}
我在Android上使用Flutter对其进行了测试,我认为它适用于原生Android,因为它们都可能使用相同的原生SDK。
推送通知包中需要有“数据”键。
除上述答案外,如果使用FCM控制台测试推送通知,则“数据”键和对象不会添加到“推送通知”包中。因此,当App处于后台或被杀时,您将不会收到详细的推送通知。
在这种情况下,您必须选择后端管理控制台来测试应用程序后台方案。
在这里,您将为推送包添加“数据”键。因此,详细推送将按预期显示。希望这对少有帮助。
如果您希望应用程序的系统托盘中只有一个通知,那么以下解决方案可以解决问题,直到FCM提出正确的解决方案。
{
notification: {
title: title,
body: body,
image: "http://path_to_image"
},
data: {
click_action: "FLUTTER_NOTIFICATION_CLICK",
your_data: ...,
},
token: token
}
<service android:name=".MyFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service>
您可以使用此休息客户端工具发送消息。使用此工具您也可以在后台和前台向客户端应用程序发送消息。要使用API发送消息,您可以使用名为AdvancedREST Client的工具,它是chrome扩展,并使用以下参数发送消息。
休息客户端工具链接: <receiver
android:name=".MyGcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="package_name" />
</intent-filter>
</receiver>
使用此URL: - https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo Content-Type:application / json授权:key =您的服务器密钥From或Authoization密钥(参见下面的参考资料)
{“data”:{“image”:“https://fcm.googleapis.com/fcm/send”,“message”:“使用API的Firebase推送消息”“AnotherActivity”:“True”},“to”:“设备ID或设备令牌”}
可以通过访问Google开发人员控制台并单击项目左侧菜单上的“凭据”按钮来获取授权密钥。在列出的API密钥中,服务器密钥将是您的授权密钥。
并且您需要将接收器的tokenID放在使用API发送的POST请求的“to”部分中。
这段android代码//消息将包含Push消息
https://static.pexels.com/photos/4825/red-love-romantic-flowers.jpg
从Firebase控制台发送大图通知:适用于后台和前台应用
而不是 String message = remoteMessage.getData().get("message1");
//imageUri will contain URL of the image to be displayed with Notification
String imageUri = remoteMessage.getData().get("image");
//If the key AnotherActivity has value as True then when the user taps on notification, in the app AnotherActivity will be opened.
//If the key AnotherActivity has value as False then when the user taps on notification, in the app MainActivity2 will be opened.
String TrueOrFlase = remoteMessage.getData().get("AnotherActivity");
//To get a Bitmap image from the URL received
bitmap = getBitmapfromUrl(imageUri);
sendNotification(message, bitmap, TrueOrFlase);
,覆盖onMessageReceived
的zzm()
并从这里创建自定义通知
FirebaseMessagingService