繁体   English   中英

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

[英]Open a different activity other than MainActivity when clicking Firebase Notification

在我的应用程序中,我想打开自定义activity (不是MainActivity ),并在单击Firebase通知时将putExtra添加到此activity
我写下面的代码,但单击通知时打开MainActivity ,但我想打开另一个活动AuctionDetailActivity )。

我的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);
        }
    }
}

我该如何解决?

更改以下行

Intent intent = new Intent(click_action);

对此

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

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

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

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

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

  • 使用FCM API发送通知,并使用data字段而不是notification字段。 查看文档以了解有关FCM API的更多信息。

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

第二种情况的示例:

@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;
}

您只需要在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());
}

更改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());

   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM