android - 在android中点击通知进入我的应用

标签 android android-intent notifications push-notification android-notifications

目前我正在研究 GCM(谷歌云消息),它允许用户将消息推送到用户设备。我想达到以下要求:

  1. 如果用户已经进入app,则忽略

  2. 如果用户没有进入应用,点击通知进入应用

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

  1. 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, 0, 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 类,如果我在主页面,它会创建一个新 Activity ,我如何调整代码以满足我的要求?

谢谢

最佳答案

为了
1.如果用户已经输入过app,则忽略:
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;
        }
    }  
  1. 如果用户没有进入应用,点击通知进入应用
    从上面的代码中,您知道是否要恢复应用程序或启动 - 调用 Splash Screen 或在您的情况下调用 WelcomeActivity。

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

关于android - 在android中点击通知进入我的应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22058422/

相关文章:

android - 从android中的相机 Intent 获取uri

java - 向 Intent 发送一个 ArrayList<ArrayList<String>>

java - 锁定屏幕上的 Android 通知

java - Android:加载 xml 布局时出现 OutOfMemoryError。我该如何避免它意味着什么?

java - 什么时候初始化 Activity 的实例变量?

java - Scala 的另一个 "Unable to instantiate activity ComponentInfo"

java - 为 Android Password Safe 应用程序选择存储数据的最佳解决方案

android - 在 Activity 上下文之外启动新的 Activity。

css - HTML5 桌面通知样式

java - 没有 switch 和条件语句的极端情况逻辑的设计模式