我正在开发一个应用程序,当店主向他发送账单时,用户会收到通知,为此,我正在使用Firebase Cloud Messaging。我收到通知,但是当我单击通知时,它没有将我重定向到我在意图方法中提到的活动而不是该活动,而是将我引导至启动器活动+主活动,因为我的启动器活动是启动画面并且它重定向主要活动。请通知我,我正在此应用程序的最后阶段。
这是MyFirebaseMessaging.java
public class MyFirebaseMessaging extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
sendNotification26API(remoteMessage);
}else {
sendNotification(remoteMessage);
}
}
private void sendNotification26API(RemoteMessage remoteMessage) {
RemoteMessage.Notification notification = remoteMessage.getNotification();
String title = notification.getTitle();
String content = notification.getBody();
int requestID = (int) System.currentTimeMillis();
Intent intent = new Intent(this, MyBookings.class);
// I have used this too
intent.putExtra(common.CONSUMER_TEXT, common.currentUser.getPhone());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,requestID,intent,PendingIntent.FLAG_UPDATE_CURRENT);
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationHelper helper = new NotificationHelper(this);
Notification.Builder builder = helper.getChannelNotification(title,content,pendingIntent,uri);
helper.getManager().notify(new Random().nextInt(),builder.build());
}
private void sendNotification(RemoteMessage remoteMessage) {
RemoteMessage.Notification notification = remoteMessage.getNotification();
Intent intent = new Intent(this, MyBookings.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,12,intent,PendingIntent.FLAG_ONE_SHOT);
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(notification.getTitle())
.setContentText(notification.getBody())
.setAutoCancel(true)
.setSound(uri)
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0,builder.build());
}
这是用于API 26以上通知的帮助程序类
public class NotificationHelper extends ContextWrapper {
private static final String MY_CHANNEL_ID ="xyz.hashtagweb.XXXXXX.XXXXXX";
private static final String MY_CHANNEL_NAME ="XXXXXXX";
private NotificationManager manager;
public NotificationHelper(Context base) {
super(base);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
createChannel();
}
}
@TargetApi(Build.VERSION_CODES.O)
private void createChannel() {
NotificationChannel channel = new NotificationChannel(MY_CHANNEL_ID,
MY_CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(false);
channel.enableVibration(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
getManager().createNotificationChannel(channel);
}
public NotificationManager getManager() {
if (manager == null )
{
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return manager;
}
@TargetApi(Build.VERSION_CODES.O)
public Notification.Builder getChannelNotification(String title, String body, PendingIntent contentIntent, Uri sound){
return new Notification.Builder(getApplicationContext(),MY_CHANNEL_ID)
.setContentIntent(contentIntent)
.setContentTitle(title)
.setContentText(body)
.setStyle(new Notification.BigTextStyle().bigText(body))
.setSmallIcon(R.mipmap.ic_launcher)
.setSound(sound)
.setAutoCancel(false);
}
}