在Android中打开活动而不单击通知

在Android中打开活动而不单击通知,android,push-notification,Android,Push Notification,是否有一种方法可以在收到推送通知时打开活动,而无需单击通知 示例:-假设我希望在收到推送通知时打开MainActivity。我可以通过单击通知面板中的通知来实现这一点。但我希望在收到通知后立即打开活动(甚至不单击通知)。有可能吗?是的(但这可能取决于您使用什么来接收推送) 在您的代码中,当接收推送时,不要构建和显示通知,而是创建并广播一个将打开您的活动的意图。在您的onMessageReceived方法中,使用以下代码创建广播 Intent intent = new Intent();

是否有一种方法可以在收到推送通知时打开活动,而无需单击通知

示例:-假设我希望在收到推送通知时打开MainActivity。我可以通过单击通知面板中的通知来实现这一点。但我希望在收到通知后立即打开活动(甚至不单击通知)。有可能吗?

是的(但这可能取决于您使用什么来接收推送)


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

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

    Intent intent = new Intent();
    intent.setAction("some string");
    //intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    sendBroadcast(intent);
Log.d("BroadCastData","BroadCastData");
    Intent newAct = new Intent(context,yourCallClassName.class);
    newAct.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(newAct);
创建一个单独的类,并使用BroadcastReceiver对其进行扩展。在onReceive内部编写以下代码

    Intent intent = new Intent();
    intent.setAction("some string");
    //intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    sendBroadcast(intent);
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>
//.GetingBroadCastData是类的名称

希望这对您有所帮助。

我知道现在有点晚了,但它对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
 >

您的setAction字符串和manifest receiver action android名称字符串应该相同。这是一个工作代码。您能分享代码片段吗?@aditya