2014-02-10 56 views
2

我在Urban Airship的幫助下發送了推送通知,並且也成功地收到了通知。但是當我點擊通知時,它不會打開我的應用程序。那麼我能做些什麼來打開我的應用程序?點擊城市飛艇推送通知,打開android應用程序?

並獲得在logcate以下錯誤: -

02-10 18:53:44.137: W/xxx - UALib(6840): Activity [email protected] was not manually added during onStart(). Call UAirship.shared().getAnalytics().activityStarted in every activity's onStart() method. 

回答

5

是做谷歌後,我得到了答案: - 我們需要如下創建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); 


    } 
} 
0

在回答您的logcat的錯誤:

UALib(6840): Activity [email protected] was not manually added during onStart(). Call UAirship.shared().getAnalytics().activityStarted in every activity's onStart() method. 

默認情況下,城市飛艇打開analyticsEnabled在AirshipConfigOptions(see documentation)。

在Android API 14之前,您必須在每個活動的onStart()方法(如logcat警告建議或see documentation)中手動調用activityStarted()。如果您的應用的minSDK版本> = 14(冰淇淋三明治),則不再需要修改任何活動。確保在airshipconfig.properties中設置minSDKVersion,以防止丟失任何檢測到的分析警告。

如果你想手動工具類,更新活動的在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); 
相關問題