单击Firebase Notification时,打开MainActivity以外的其他活动

时间:2018-09-30 06:08:21

标签: java android firebase android-notifications

在我的应用程序中,当我点击 Firebase 时,我想为此activity打开自定义putExtra(不是 MainActivity )和activity通知。
我编写以下代码,但是当单击通知时打开 MainActivity ,但是我想打开我的另一项活动 AuctionDetailActivity )。
< / p>

我的NotificationManager类:

public class MyNotificationManager {

    private Context mCtx;
    private Uri soundUri;
    private static MyNotificationManager mInstance;

    public MyNotificationManager(Context context) {
        mCtx = context;
    }

    public static synchronized MyNotificationManager getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new MyNotificationManager(context);
        }
        return mInstance;
    }

    public void displayNotification(String title, String body) {

        soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        Intent intent = new Intent(mCtx, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("fcm_notification", "Y");

        PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx, Constants.NOTIF_CHANNEL_ID)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setSound(soundUri)
                .setAutoCancel(true)
                .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                .setContentText(body)
                .setContentIntent(pendingIntent);

        NotificationManager mNotifyMgr = (NotificationManager) mCtx.getSystemService(NOTIFICATION_SERVICE);

        if (mNotifyMgr != null) {
            mNotifyMgr.notify(1, mBuilder.build());
        }
    }
}

和MyFirebaseMessagingService类:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        showNotify(remoteMessage.getFrom(), remoteMessage.getNotification().getBody());
    }

    private void showNotify(String title, String body) {
        MyNotificationManager myNotificationManager = new MyNotificationManager(getApplicationContext());
        //myNotificationManager.displayNotification(title, body);
        myNotificationManager.displayNotification(title, body);
    }
}

MainActivity代码:

@Override
protected void onResume() {
    super.onResume();
    String fcm_notification = getIntent().getStringExtra("fcm_notification");
    Log.d("FireBaseIntentLog", " FCM : " + fcm_notification);
    if (getIntent().getExtras() != null) {
        for (String key : getIntent().getExtras().keySet()) {
            String value = getIntent().getExtras().getString(key);
            Log.d("FireBaseIntentLog", "Key: " + key + " Value: " + value + " FCM : " + fcm_notification);
        }
    }
}

我该如何解决?

4 个答案:

答案 0 :(得分:1)

在下面的行中更改

Intent intent = new Intent(click_action);

对此

Intent intent = new Intent(getActivity(), YourClass.class);

答案 1 :(得分:1)

如果您是使用FCM API从Firebase控制台或notification字段内发送通知,则该应用的行为有两种-

  • 如果您的应用程序位于前台,则将调用FCM服务类的方法onMessageReceived
  • 如果您的应用程序在后台运行,则FCM服务类内部将不会发生任何事情。而是由FCM库本身在内部处理该通知,并显示意图中具有启动程序活动的通知。

如果您使用FCM API发送通知并使用data字段,则库本身不执行任何操作,而是调用方法onMessageReceived,无论您的应用程序是前台还是后台。

因此,为了解决您的问题,您可以使用以下两种解决方案之一:

  • 使用FCM API发送通知,并使用data字段代替notification字段。检查documentation,以了解有关FCM API的更多信息。

  • 在启动器(主)活动中,检查onCreate中的意图,如果它来自通知,请阅读其他内容,完成主要活动并打开所需的活动。

    < / li>

第二种情况的示例:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (checkIntent()) return;

    // other code.
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    checkIntent();
}

private boolean checkIntent() {
    // to receive the value, send the value as custom data from Firebase console.
    String value = getIntent().getStringExtra("your_key");

    if (value == null) return false;

    if (value.equals("something")) {
        // open one activity.

    } else if (value.equals("another_thing")) {
        // open another activity.
    }

    finish();
    return true;
}

答案 2 :(得分:0)

您只需要在sendNotification函数中进行修改

public void sendNotification(String messageBody, String messageTitle, int user_id, String click_action) {
    Intent intent = new Intent(mCtx, AuctionDetailActivity.class); // Need modify this line
    intent.putExtra(Extras.bidID.name(), user_id);

    PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mCtx, Constants.NOTIF_CHANNEL_ID);
    notificationBuilder.setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());
}

答案 3 :(得分:0)

如下所示更改MyFirebaseMessagingService类,用活动名称替换OtherApp.class

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
    Intent intent=new Intent(this,OtherApp.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);//newbg PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder notificationBuilder= new NotificationCompat.Builder(this);
    notificationBuilder.setContentTitle("FCM NOTIFICATION"); notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
    notificationBuilder.setContentIntent(pendingIntent);
    NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0,notificationBuilder.build());

   }
}