当我从后端发送通知时,我同时接收到notification
有效负载和data
有效负载。现在,当从后端触发通知时,会自动接收默认通知。
此(默认)即将到来的通知是否可以被覆盖(如UI更改或未决的意图更改)?
[我尝试同时使用notification
有效负载和data
有效负载(后来我知道当应用程序为后台时通知有效负载不起作用),但最终没有任何帮助。
作为最后的选择,我正在考虑不发送任何notification
有效负载数据,而仅发送data
有效负载中的必需通知数据-这样可以吗?
为此附加代码段。(我最后一次尝试使用datapayload一个值)
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);
}