繁体   English   中英

在Android中无需单击通知即可打开活动

[英]Open Activity without clicking notification in Android

有没有办法在不点击通知的情况下打开接收推送通知的活动?

示例:- 假设我想在收到推送通知时打开 MainActivity。 我可以通过单击通知面板中的通知来做到这一点。 但是我想在收到通知后立即打开该活动(甚至无需单击通知)。 是否可以 ??

在您的onMessageReceived方法中,使用以下代码创建广播

    Intent intent = new Intent();
    intent.setAction("some string");
    //intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    sendBroadcast(intent);

创建一个单独的类并使用BroadcastReceiver扩展它。 onReceive里面写下面的代码

Log.d("BroadCastData","BroadCastData");
    Intent newAct = new Intent(context,yourCallClassName.class);
    newAct.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(newAct);

将接收器添加到清单文件的最后一步

<receiver android:name=".GetingBroadCastData" >  //.GetingBroadCastData is classs name
        <intent-filter>
            <action android:name="some string" />
        </intent-filter>
    </receiver>

愿这对你有帮助。

是的(但这可能取决于您用于接收推送的内容)。

在您的代码中,当接收推送时,不是构建和显示通知,而是创建和广播将打开您的活动的意图。

我知道这有点晚了,但它对 firebase 类中的 firebase 内的其他人很有用。

 public class FireMsgService extends FirebaseMessagingService {
        public static final String NOTIFICATION_CHANNEL_ID = "10001" ;
        private final static String default_notification_channel_id = "default" ;
        String toke;
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            super.onMessageReceived(remoteMessage);
            String message = remoteMessage.getNotification().getBody();
    }

创建一个广播接收器并添加以下代码。

public class OnMessageRecieverBroadCast extends BroadcastReceiver {
        public static final String NOTIFICATION_CHANNEL_ID = "10001";
        public static String NOTIFICATION_ID = "notification-id";
        public static String NOTIFICATION = "notification";
        public static String NOTIFICATION_MSG = "notificationmsg";
        String MsgTitle = "";
        String MsgBody;
        public void onReceive(Context context, Intent intent) {
            if (intent.getExtras() != null) {
                Log.d("TestTag", "keySet() size" + intent.getExtras().keySet().size());
                for (String key : intent.getExtras().keySet()) {
                     Object value = intent.getExtras().get(key);
                     MsgBody = (String) intent.getExtras().get("gcm.notification.body");
                     MsgTitle = (String) intent.getExtras().get("gcm.notification.title");
                     Intent intent1 = new Intent(context, NotificationDialogActivity.class);
                     intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                     intent1.putExtra("value", MsgBody);
                     intent1.putExtra("NOTIFICATION_MSG_TITLE", MsgTitle);
                     context.startActivity(intent1);
            }
        }
    }

onCreate方法中。

onCreate(){
         if (getIntent().getExtras() != null) {
            toke = getIntent().getStringExtra("value");
            dialogOpenTop(NotificationDialogActivity.this, toke);
           }
        //add dialog method
        public void dialogOpenTop(final Context context, String toke) {
        final Dialog dialog = new Dialog(NotificationDialogActivity.this);
        dialog.setContentView(R.layout.alert);
        dialog.getWindow().setBackgroundDrawable(new 
        ColorDrawable(Color.TRANSPARENT));
        dialog.setCancelable(false);
        dialog.show    
       }

在清单中:

 <service
        android:name=".notification.FireMsgService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    <receiver
        android:name=".notification.OnMessageRecieverBroadCast"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </receiver
 >

暂无
暂无

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

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