gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:38:46 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com