How do I tell if the app was opened or resumed as a result of a push notification in Titanium?

假如想象 提交于 2020-01-01 05:38:06

问题


How do you handle a push differently in Titanium depending on whether the app was open at the time the push arrived?

When a push notification comes in and my app is running, I would like to pop up a message offering to take the user to the item the notification refers to. If the app is closed, and the user taps on the push notice and causes the app to open, I would like to take the user directly to the item without a popup.

Titanium's registerForPushNotifications seems to only have one callback, "callback", which is called regardless of the app's state when the push arrives. According to How do I tell if my iPhone app is running when a Push Notification is received? you can use didReceiveRemoteNotification and didFinishLaunchingWithOptions in Objective C, but Titanium doesn't seem to provide separate access to those.


回答1:


The 'callback' function will get fired right after 'resume' if user comes from a notification.

So I would handle your case the following way:

have a variable to track wether app is paused or not (a la var is_paused = false; when running). is_paused would be switched to true upon Ti.App 'pause' event and switched back to false state with a timeout (1second will do) upon Ti.App 'resume' event.

Then you can have different functionality in callback function by checking if is_paused var is true or false:

//in notification callback
if(is_paused){ 
   //user is coming from background (do your thing automatically)
}else{
  //user is in app (display the alert)
}


来源:https://stackoverflow.com/questions/12057576/how-do-i-tell-if-the-app-was-opened-or-resumed-as-a-result-of-a-push-notificatio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!