如何判断应用程序是否因 Titanium 中的推送通知而被打开或恢复? [英] How do I tell if the app was opened or resumed as a result of a push notification in Titanium?

查看:20
本文介绍了如何判断应用程序是否因 Titanium 中的推送通知而被打开或恢复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据推送到达时应用是否打开,您如何在 Titanium 中以不同方式处理推送?

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 的 registerForPushNotifications 似乎只有一个回调,回调",当推送到达时,无论应用程序的状态如何,都会调用该回调.根据 如何在收到推送通知时判断我的 iPhone 应用程序是否正在运行? 您可以在 Objective C 中使用 didReceiveRemoteNotification 和 didFinishLaunchingWithOptions,但 Titanium 似乎没有提供对这些的单独访问.

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.

推荐答案

如果用户收到通知,回调"函数将在恢复"后立即触发.

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

所以我会用以下方式处理你的案子:

So I would handle your case the following way:

有一个变量来跟踪应用程序是否暂停(运行时).is_paused 将在 Ti.App 'pause' 事件时切换为 true 并在 Ti.App 'resume' 事件时切换回假状态并超时(1 秒即可).

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.

然后你可以通过检查 is_paused var 是真还是假来在回调函数中拥有不同的功能:

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

这篇关于如何判断应用程序是否因 Titanium 中的推送通知而被打开或恢复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆