单击城市飞艇的推送通知打开Android应用程序?

时间:2014-02-10 13:19:42

标签: android push-notification urbanairship.com

我在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.

2 个答案:

答案 0 :(得分:5)

在做Google之后我得到了答案: - 我们需要创建一个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);


    }
}

答案 1 :(得分:0)

响应您的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&gt; = 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);
}

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

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