【发布时间】:2018-01-26 08:17:27
【问题描述】:
我在我的项目中使用 firebase 推送通知。但每当我点击通知时,else 部分总是会显示。我想在点击通知时在该对话框中显示通知文本。
这是我在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();
}
【问题讨论】:
标签: android firebase notifications firebase-cloud-messaging firebase-notifications