我使用FireBase可以在我的應用程序中進行消息傳遞,並且我希望當用戶收到消息時活動片段發生變化。我做了下面的代碼,但我不知道爲什麼它會給我getFragmentManager上的錯誤,因爲我沒有活動上下文或類似的東西。如何在FirebaseMessagingService中啓動提交片段
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());
}
}
我知道並說getFragmentManager是活動的一部分,但我怎麼能綁定它 – AndroidDev
這裏可能有一個答案:http://stackoverflow.com/a/37322977/3527024 希望它有幫助。 – thenaoh