单击推送通知时如何打开片段 [英] How to open Fragment on click of push notification

查看:62
本文介绍了单击推送通知时如何打开片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Firebasemessagingservice类代码,用于传递意图:

My Firebasemessagingservice class code for passing intent:

private void showNotification(String message){
    Intent intent=new Intent(this, DrawerActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("data", message);
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder=new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle("Registry")
            .setContentText(message)
            .setSmallIcon(R.drawable.com_facebook_button_send_icon_blue)
            .setContentIntent(pendingIntent);
    NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(0,builder.build());
}

onCreate()方法中的我的片段BaseActivity代码:

My Fragment BaseActivity code in onCreate() method:

Intent notifyIntent = getIntent();
    Bundle extras = notifyIntent.getExtras();
    if (extras != null) {

       replacefragment code;

    }

它不起作用.

推荐答案

您需要发送包含意图数据的密钥,并检查该密钥并执行类似的代码

You need to send a key with intent data and check with that key and perform code like

private void showNotification(String message){
    Intent intent=new Intent(this, DrawerActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("data", message);
     intent.putExtra("KEY", "YOUR VAL");
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder=new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle("Registry")
            .setContentText(message)
            .setSmallIcon(R.drawable.com_facebook_button_send_icon_blue)
            .setContentIntent(pendingIntent);
    NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(0,builder.build());
}

并喜欢"Activity"之类的

and chech on Activity like

Intent notifyIntent = getIntent();
   String extras = getIntent().getExtraString("KEY");;
    if (extras != null&&extras.equals("YOUR VAL")) {`enter code here`

       replacefragment code;

    }

还要检查活动的意图更改是否已经打开

also check onIntent change fro if activity is already open

这篇关于单击推送通知时如何打开片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆