【问题标题】:Open android app on clicking on the push notification with urban airship?点击城市飞艇的推送通知打开android应用程序?
【发布时间】:2014-02-10 13:19:41
【问题描述】:

我正在 Urban Airship 的帮助下发送推送通知,并且也成功收到了通知。但是当我点击通知时,它并没有打开我的应用程序。那么打开我的应用该怎么做呢?

并在 logcate 中收到以下错误:-

02-10 18:53:44.137: W/xxx - UALib(6840): Activity com.aaa.yyy.SplashActivity@40dcb458 was not manually added during onStart(). Call UAirship.shared().getAnalytics().activityStarted in every activity's onStart() method.

【问题讨论】:

    标签: android push-notification urbanairship.com


    【解决方案1】:

    是的,在谷歌之后我得到了答案:- 我们需要创建一个 IntentReceiver.java 类如下:-

     public class IntentReceiver extends BroadcastReceiver{
     private static final String logTag = "PushSample";
     @Override
     public void onReceive(Context context, Intent intent) {
        Log.i(logTag, "Received intent: " + intent.toString());
        String action = intent.getAction();
    
         if (action.equals(PushManager.ACTION_PUSH_RECEIVED)) {
             int id = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, 0);
             Log.i(logTag, "Received push notification. Alert: "
                        + intent.getStringExtra(PushManager.EXTRA_ALERT)
                        + " [NotificationID="+id+"]");
    
                logPushExtras(intent);
         }else if (action.equals(PushManager.ACTION_NOTIFICATION_OPENED)) {
              Log.i(logTag, "User clicked notification. Message: " + intent.getStringExtra(PushManager.EXTRA_ALERT));
    
              logPushExtras(intent);
    
              Intent launch = new Intent(Intent.ACTION_MAIN);
              launch.setClass(UAirship.shared().getApplicationContext(), MainActivity.class);
              launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              UAirship.shared().getApplicationContext().startActivity(launch);
    
        }else if (action.equals(PushManager.ACTION_REGISTRATION_FINISHED)) {
            Log.i(logTag, "Registration complete. APID:" + intent.getStringExtra(PushManager.EXTRA_APID)
                    + ". Valid: " + intent.getBooleanExtra(PushManager.EXTRA_REGISTRATION_VALID, false));
        }
    }
    
    private void logPushExtras(Intent intent) {
        Set<String> keys = intent .getExtras().keySet();
        for(String key: keys){
            List<String> ignoredKeys = (List<String>)Arrays.asList("collapse_key", "from", PushManager.EXTRA_NOTIFICATION_ID,PushManager.EXTRA_PUSH_ID, PushManager.EXTRA_ALERT);
            if(ignoredKeys.contains(key)){
                continue;
            }
             Log.i(logTag, "Push Notification Extra: ["+key+" : " + intent.getStringExtra(key) + "]");
        }   }   
      }
    

    之后我们需要调用PushNotification.java中的如下方法。 这是代码。

    public class PushNotification extends Application{
    
        @Override
        public void onCreate() {
    
            AirshipConfigOptions options = AirshipConfigOptions.loadDefaultOptions(this);
            options.developmentAppKey = "xxx";
            options.developmentAppSecret = "yyy";
            options.productionAppKey = "zzz";
            options.inProduction= false;
    
            UAirship.takeOff(this, options);
            PushManager.enablePush();
    
            String apid = PushManager.shared().getAPID();
            Logger.info("My Application onCreate - App APID: " + apid);
    
            PushManager.shared().setIntentReceiver(IntentReceiver.class);
    
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      响应您的 logcat 错误:

      UALib(6840): Activity com.aaa.yyy.SplashActivity@40dcb458 was not manually added during onStart(). Call UAirship.shared().getAnalytics().activityStarted in every activity's onStart() method.
      

      默认情况下,Urban Airship 在 AirshipConfigOptions (see documentation) 中打开 analyticsEnabled。

      在 Android API 14 之前,您必须在每个活动的 onStart() 方法中手动调用 activityStarted()(如 logcat 警告或 see documentation 所示)。如果您的应用的 minSDKVersion >= 14(冰淇淋三明治),您不再需要修改任何活动。确保在 airshipconfig.properties 中设置 minSDKVersion 以防止丢失任何检测分析警告。

      如果您想手动检测您的类,请使用以下内容更新您的 Activity 的 onStart 和 onStop 方法:

      @Override
      public void onStart() {
          super.onStart();
          Analytics.activityStarted(this);
      }
      
      @Override
      public void onStop() {
          super.onStop();
          Analytics.activityStopped(this);
      }
      

      注意: 根据UA v4 to v5 migration guide,报告活动开始和停止的方法现在是静态的(这是上面的示例代码)。如果您需要支持旧版本的 UA,那么您可以使用 logcat 中的示例代码:

      UAirship.shared().getAnalytics().activityStarted(this);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多