Open Activity without clicking notification in And

2019-08-09 23:40发布

问题:

Is there a way to open an activity on receiving a push notification without clicking on the notification ?

Example :- Say I want to open the MainActivity whenever I receive a push notification. I can do that by clicking on the notification in the notification panel. But I want to open the activity as soon as I receive the notification (without even clicking the notification). Is it possible ??

回答1:

Yes (but that may be depend on what you use to receive the push).

In your code, when receiveing the push, instead of building and displaying a notification, create and broadcast an intent that will open your activity.



回答2:

in your onMessageReceived method, create Broadcast using following code

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

Make a seperate class and extends it with BroadcastReceiver. Inside onReceive write following code

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

Last step to add receiver into manifests file

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

May this help you.