【发布时间】:2017-03-16 11:15:47
【问题描述】:
我使用 FireBase 可以在我的应用程序中发送消息,并且我希望当用户收到消息时活动片段发生变化。我为此做了以下代码,但我不知道为什么它在 getFragmentManager 上给我错误,因为我没有活动上下文或类似的东西。
public class googleFirebaseMessageService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
switch (remoteMessage.getData().get("message"))
{
case "invoices_ready":
SharedPreferences preferences=getSharedPreferences("invoices_ready",MODE_PRIVATE);
SharedPreferences.Editor editor=preferences.edit();
editor.putString("pr_id",remoteMessage.getData().get("pr_id").toString());
editor.commit();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.root_menu_fragment, new _step4_FragmentDrugInfo());
transaction.addToBackStack("mapView");
transaction.commit();
showNotification(remoteMessage.getData().get("message"));
break;
}
}
private void showNotification(String messageBody) {
Intent intent = new Intent( this , splash.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Android Tutorial Point FCM Tutorial")
.setContentText(messageBody)
.setAutoCancel( true )
.setSound(notificationSoundURI)
.setContentIntent(resultIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotificationBuilder.build());
}
}
【问题讨论】:
标签: android android-fragments firebase android-notifications firebase-cloud-messaging