当我从后端发送通知时,我同时收到notification
有效负载和data
有效负载。现在,当从后端触发通知时,会自动接收默认通知。
是否可以覆盖此(默认(即将发布的通知(例如 UI 更改或挂起的意图更改(?
我尝试使用notification
有效负载,然后使用data
有效负载(因为后来我知道当应用程序是后台时通知有效负载不起作用(, 但最终什么都不适合我。
作为最后的手段,我正在考虑不发送任何notification
有效载荷数据,而只在有效负载中发送data
通知数据 - 这会起作用吗?
附加代码截图与此.. (我最后一次尝试使用数据有效载荷一个值(
public class AppFcmReceiverData extends FirebaseMessagingService {
private static final String CHANNEL_ID = "ds";
private static final String TAG = "4";
private int numMessages = 0;
private Context mContext;
private String newsid;
private NotificationManagerCompat notificationManager;
private NotificationUtils notificationUtils;
/*
private ArrayList<NewsData.notificationData> notificationDatalist;
*/
NewsData newsData;
private Map<String, String> notificationdata;
@Override
public void onMessageReceived(@NotNull RemoteMessage remoteMessage) {
mContext=AppFcmReceiverData.this;
if (remoteMessage == null)
return;
if (remoteMessage.getNotification() != null) {
Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody().toString());
handleNotification(remoteMessage.getNotification().getBody());
}
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
handleDataMessage(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}
private void handleNotification(String body) {
// app is in foreground, broadcast the push message
Intent pushNotification = new Intent(Constants.PUSH_NOTIFICATION);
pushNotification.putExtra("message", body);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
}
private void handleDataMessage(JSONObject json) {
Log.e(TAG, "push json: " + json.toString())
...................................................................
public class NotificationUtils {
private static String TAG = NotificationUtils.class.getSimpleName();
private Context mContext;
public NotificationUtils(Context mContext) {
this.mContext = mContext;
}
public void showNotificationMessage(String title, Intent intent1, Intent intent) {
showNotificationMessage(title,intent,null);
}
public void showNotificationMessage(final String title,Intent intent) {
if (TextUtils.isEmpty(title))
return;
// notification icon
final int icon = R.mipmap.ic_launcher;
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
);
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
final Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + mContext.getPackageName() + "/raw/notification");
showSmallNotification(mBuilder, icon, title/*, message, timeStamp,*/ ,resultPendingIntent, alarmSound);
}
private void showSmallNotification(NotificationCompat.Builder mBuilder, int icon, String title,/* String message, String timeStamp, */PendingIntent resultPendingIntent, Uri alarmSound) {
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
// inboxStyle.addLine(message);
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(inboxStyle)
//.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
// .setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Integer.parseInt(String.valueOf(Constants.NOTIFICATION_ID)), notification);
}
是的,这是可能的。您可以省略notification
有效负载,只需在从控制台/管理员开发工具包发送 FCM 时填充data
有效负载。
您可以覆盖 android 应用程序服务上的onMessageReceived(RemoteMessage remoteMessage)
- 您可以在其中接收和解析data
有效负载,并使用自定义挂起的意图构建自定义通知。请参阅此处的示例。
您可以在此处找到有关如何在 android 设备上构建和显示自定义通知的详细信息。