微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

点击通知在android中输入我的应用程序

目前我正在开发GCM(Google Cloud消息),它允许用户将消息推送到用户设备.我想达到以下要求:

>如果用户已输入app,请忽略它
>如果用户未输入应用程序,请单击通知以进入应用程序

我的应用程序的工作流程是:

> WelcomePage(从中下载json并创建数据集)=> MainPage(基于数据集显示)

处理通知代码

private void sendNotification(String msg) {
        mnotificationmanager = (notificationmanager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        String notifyMsg = "";
        JSONTokener tokener = new JSONTokener(msg);

        if (tokener != null) {
            try {
                notifyMsg = new JSONObject(tokener).getString("msg");
            } catch (JSONException e) {
                // Todo Auto-generated catch block
                e.printstacktrace();
            }
        }

        Intent myintent = new Intent(this,WelcomePageActivity.class);
        myintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(this,myintent,PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(getResources().getString(R.string.notification_title))
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(notifyMsg))
        .setContentText(notifyMsg)
        .setContentIntent(contentIntent);

        mnotificationmanager.notify(NOTIFICATION_ID,mBuilder.build());
    }

问题是如果我使用WelcomePageActivity类,如果我在主页面,它将创建一个新活动,我如何调整代码以满足我的要求?

谢谢

解决方法

对于
1.如果用户已经输入应用程序,请忽略它:
在onReceive()中,检查您的应用是否正在运行,不要通知.
可以通过以下方式检查:
ActivityManager activityManager =(ActivityManager)gpsService.this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList= activityManager.getRunningServices(Integer.MAX_VALUE);

if((serviceList.size() > 0)) {
    boolean found = false;

    for(int i = 0; i < serviceList.size(); i++) {
        RunningServiceInfo serviceInfo = serviceList.get(i);
        ComponentName serviceName = serviceInfo.service;

        if(serviceName.getClassName().equals("Packagename.ActivityOrServiceName")) {
            //Your service or activity is running
            break;
        }
    }

>如果用户未输入应用程序,请单击通知以进入应用程序
从上面的代码中,您知道是否要恢复应用程序或启动 – 调用启动画面或您的案例WelcomeActivity.

关于您的应用程序的工作流程,我建议您检查是否需要每次都下载数据.可以保存它或仅在需要时更新/下载,其余流程按原样工作.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐