我在应用中使用FCM通知,我使用云功能侦听特定节点并按主题发送通知并在android中订阅主题以接收消息
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String notificationTitle = null, notificationBody = null;
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
notificationTitle = remoteMessage.getNotification().getTitle();
notificationBody = remoteMessage.getNotification().getBody();
}
createNotification(notificationTitle,notificationBody);
}
private NotificationManager notifManager;
public void createNotification(String title,String aMessage) {
final int NOTIFY_ID = 0; // ID of notification
String id = "asd";// default_channel_id
// Default Channel
Intent intent;
PendingIntent pendingIntent;
NotificationCompat.Builder builder;
if (notifManager == null) {
notifManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = notifManager.getNotificationChannel(id);
if (mChannel == null) {
mChannel = new NotificationChannel(id, title, importance);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notifManager.createNotificationChannel(mChannel);
}
builder = new NotificationCompat.Builder(this, id);
intent = new Intent(this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentTitle(title) // required
.setSmallIcon(android.R.drawable.ic_popup_reminder) // required
.setContentText(aMessage) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setTicker(aMessage)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
}
else {
builder = new NotificationCompat.Builder(this, id);
intent = new Intent(this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentTitle(title) // required
.setSmallIcon(android.R.drawable.ic_popup_reminder) // required
.setContentText(aMessage) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setTicker(aMessage)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
.setPriority(Notification.PRIORITY_HIGH)
.setLargeIcon(BitmapFactory.decodeResource
(getResources(), R.drawable.logo))
.setBadgeIconType(R.drawable.logo)
.setStyle(new NotificationCompat.BigTextStyle().setBigContentTitle(title).bigText(aMessage));
;
}
Notification notification = builder.build();
notifManager.notify(NOTIFY_ID, notification);
}
它在许多支持Oreo的设备上都运行良好,但如果应用程序处于前台和后台,则无法在Samsung note 9中运行 有帮助吗?