How do I tell if the app was opened or resumed as

2019-03-30 05:58发布

问题:

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)
}