簡體   English   中英

使用Urban Airship推送通知在首次啟動時將apID返回為null

[英]Push Notifications using Urban Airship returns apID as null at the first launch

我已成功將城市飛艇推送通知集成到我的Android應用程序中。 我遇到了一個問題,就是apID在首次啟動時變為null,然后在第二次啟動時,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類。 任何幫助表示贊賞。

我發現原因是延遲,為了得到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);

        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM