我在项目中使用了Firebase推送通知。但是每当我单击通知时,都会始终显示其他部分。我想在单击通知时在该对话框中显示通知文本。
这是我在NotifService.class
的代码:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.icon_main)
.setContentTitle("Update from Refer Ncert")
.setContentText(""+remoteMessage.getNotification().getBody())
.setAutoCancel(true)
.setLights(Notification.DEFAULT_LIGHTS,1000,20000)
.setVibrate(new long[] { 1000, 1000})
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("notifmsg",remoteMessage.getNotification().getBody());
PendingIntent pi = PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
}
以下是 MainActivity onCreate()
中用于显示通知的代码:
String notifmsg=getIntent().getStringExtra("notifmsg");
if(notifmsg != null){
new SweetAlertDialog(this)
.setTitleText("Hello Readers!")
.setContentText(notifmsg)
.show();
}
else{
new SweetAlertDialog(this)
.setTitleText("Welcome Back")
.setContentText("Continue reading your previous book by clicking on RECENT BOOK button below.")
.show();
}
据我所知,您定义了意图,但不会"发送"它。
在定义挂起的意图后,将其添加到代码代码中,它应该可以工作:
pi.send();
你可以使用这个:
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("Notification")
.setContentText(messageBody)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());