【问题标题】:Push Notifications using Urban Airship returns apID as null at the first launch使用 Urban Airship 的推送通知在第一次启动时返回 apID 为 null
【发布时间】:2013-11-18 14:54:03
【问题描述】:

我已经成功地将城市飞艇推送通知集成到我的安卓应用程序中。我在这里遇到了一个问题,即第一次启动时 apID 变为空,然后在第二次启动时,apId 正确。

这就是我获取 apId 的方式:

String apid = PushManager.shared().getAPID();

这是我用过的类:

public class MyApplication extends Application {

    public static SharedPreferences PREFS_TOKEN;
    public static final String PREFERENCE = "UrbanAirship";
    public static String tokenKey;

       @Override
       public void onCreate(){
           AirshipConfigOptions options = AirshipConfigOptions.loadDefaultOptions(this);
           UAirship.takeOff(this, options);
           PushManager.enablePush();
           Logger.logLevel = Log.VERBOSE;

            // use CustomPushNotificationBuilder to specify a custom layout
            CustomPushNotificationBuilder nb = new CustomPushNotificationBuilder();

            nb.statusBarIconDrawableId = R.drawable.paw;// custom status bar icon

            nb.layout = R.layout.notification;
            nb.layoutIconDrawableId = R.drawable.paw;// custom layout icon
            nb.layoutIconId = R.id.icon;
            nb.layoutSubjectId = R.id.subject;
            nb.layoutMessageId = R.id.message;

            // customize the sound played when a push is received
            // nb.soundUri =
            // Uri.parse("android.resource://"+this.getPackageName()+"/"
            // +R.raw.cat);

            String apid = PushManager.shared().getAPID();

            Logger.info("My Application onCreate - App APID: " + apid);

            PREFS_TOKEN = this.getSharedPreferences(PREFERENCE,
                    Context.MODE_PRIVATE);
            Editor edit = PREFS_TOKEN.edit();

            if (apid != null && apid != "") {
                edit.putString(PREFERENCE, apid);
                edit.commit();
            } else {
                edit.putString(PREFERENCE, apid);
                edit.commit();
            }

            tokenKey = PREFS_TOKEN.getString(PREFERENCE, "");
            System.out.println("---------- - App APID: " + tokenKey);

            PushManager.shared().setNotificationBuilder(nb);
            PushManager.shared().setIntentReceiver(IntentReceiver.class);
       }
    }

我还有一个 IntentReceiver 类。任何帮助表示赞赏。

【问题讨论】:

  • 获取 null apid 时是否收到任何错误/消息?
  • 不是错误消息,只是在第一次启动时为空。

标签: android null urbanairship.com


【解决方案1】:

我发现原因是延迟,要获取 apId,需要一些时间,因此,在您的 IntentReceiver 类中,您必须等待 `PushManager.ACTION_REGISTRATION_FINISHED 触发。从那里,您可以获取 apId:

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));
            // Notify any app-specific listeners
            String apid = PushManager.shared().getAPID();


            Intent launch = new Intent(UAirship.getPackageName()+ APID_UPDATED_ACTION_SUFFIX);
            UAirship.shared().getApplicationContext().sendBroadcast(launch);

        }

【讨论】:

  • 如何获得“动作”值?
  • 如何获取“action”和 APID_UPDATED_ACTION_SUFFIX 值?
  • 如何获取“action”和 APID_UPDATED_ACTION_SUFFIX 值?
  • 这是一个 Sting,但不确定它的作用。请参考:stackoverflow.com/questions/17921968/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-10
相关资源
最近更新 更多