在Android中打开活动而不点击通知 [英] Open Activity without clicking notification in Android

查看:93
本文介绍了在Android中打开活动而不点击通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不单击通知的情况下打开收到推送通知的活动?

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

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

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 ??

推荐答案

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

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);

制作一个单独的类,并使用 BroadcastReceiver 对其进行扩展.在 onReceive 内部编写以下代码

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>

这对您有帮助吗?

这篇关于在Android中打开活动而不点击通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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