android - 单击 Urban Airship 的推送通知打开 Android 应用程序?

标签 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.

最佳答案

是的,在谷歌之后我得到了答案:- 我们需要创建一个 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);


    }
}

关于android - 单击 Urban Airship 的推送通知打开 Android 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21678395/

相关文章:

Android - 如何为约束布局参数设置动画?

ios - 是否可以使用 firebase 调用云函数中的其他 API?

iphone - 制作应用 Urban Airship

android - E/Push 示例 - PackageManager 未知 UALib : Required permission com. google.android.c2dm.permission.RECEIVE

java - android开发的屏幕分辨率有多大问题

android-如何为所有 cpu 架构编译 ffmpeg

java - 最有效的图像显示方式

ios - 在 Xamarin 上推送通知的 Web API - 流程是什么?

iphone - 我的应用程序仅请求推送通知权限,但不请求位置。我该如何解决这个问题?

iOS - 如何在 iOS 应用程序上跟踪用户对推送通知的选择?