我们如何在发送通知方法中调用MyFirebaseMessagingService中的片段?

时间:2017-04-03 12:45:12

标签: android firebase-authentication

我想从firebase中的发送通知方法调用片段

 private void sendNotification(String messageBody) {
        DataManager.factory().notIfArray.add(new NotIfModel(messageBody));

        //FragmentManager fragmentManager = ((FragmentActivity) context).getSupportFragmentManager();

        Intent intent = new Intent(this, Dashboard.class);

       // fragmentManager.beginTransaction().replace(R.id.container,new NotIfFragment()).addToBackStack("").commit();
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_vems)
                .setContentTitle(getBaseContext().getString(R.string.app_name))
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

        synchronized (notificationManager) {
            notificationManager.notify();
        }

我想调用片段

,而不是在Intent中使用Dashboard

2 个答案:

答案 0 :(得分:0)

您需要像往常一样启动基本活动,其中包含片段,并且您可以在onCreate()方法中加载片段。

@Override
protected void onCreate(final Bundle savedInstanceState)
{
       FragmentManager fragmentManager = getFragmentManager();
       FragmentTransaction fragmentTransaction = 
                       fragmentManager.beginTransaction();


         // Activity was not launched with a menuFragment selected -- continue as //if this activity was opened from a launcher (for example)
         HomeFragment homeFragment = new HomeFragment ();
         fragmentTransaction.replace(android.R.id.content, homeFragment );

}

答案 1 :(得分:0)

我们知道Fragments是Activity的子部分......所以要显示你必须调用Activity的任何内容,你可以在活动中替换或添加片段以显示通知。

所以我的建议是调用MyFireBaceService中的活动,在该活动中,您可以通过替换和添加来显示您的片段。 :)

我希望它能帮到你