简体   繁体   English

Ionic 2中的通知

[英]Notifications in Ionic 2

I have gone through many posts. 我已经看了很多帖子。

Its bit confusing to understand that does ionic 2 app send local-notification even if my-app-is-killed-by-user or not ? 有点困惑的是,即使my-app被用户杀死了,ionic 2 app也会发送本地通知吗?

I have below scenario. 我有以下情况。

Assuming that app-is-killed-by-user 假设应用被用户杀死

Can we run the app in background (like after 24 hours check if some conditions are true by manipulating local/cloud storage) then send local notification ? 我们能否在后台运行该应用程序(例如24小时后通过操作本地/云存储来检查某些条件是否成立)然后发送本地通知?

I am new to ionic 2. So can anyone let me know that above scenario is possible ? 我是离子2的新手。那么有人可以让我知道上述情况是可能的吗?

If not possible then let me know that can we manipulate (get and analyse) local-storage via push notification server (assuming that app-is-killed-by-user) ? 如果无法实现,那么让我知道我们可以通过推送通知服务器(假设该应用被用户杀死)来操纵(获取和分析) 本地存储吗?

Both are possible. 两者都有可能。 I'll tell you how i would do them and not share code (it can be long), i'll give you the steps and you can figure it out. 我将告诉您我将如何做而不共享代码(可能会很长),我将为您提供步骤,您可以弄清楚。

For the first scenario you'll need LocalNotification plugin , LocalStorage , Platform import from 'ionic-angular' and MomentJS (not required, but highly recommended). 对于第一种情况,您需要从'ionic-angular'和MomentJS导入LocalNotification pluginLocalStoragePlatform (不是必需的,但强烈建议)。

  • You'll need to create a service, at least it's good so you don't need to put everything in your app.components . 您需要创建一个服务,至少它很好,因此您无需将所有内容都放入app.components
  • You can use 3 of the Platform methods to verify and set the LocalNotification , they're .pause , .resume and .ready . 您可以使用3种Platform方法来验证和设置LocalNotification ,它们是.pause.resume.ready
  • Pause will check if the user has left the app, but it's still running on background. 暂停将检查用户是否已离开该应用程序,但该应用程序仍在后台运行。
  • Resume will check if the user has come back to the app, but'll not trigger if the app was killed. 简历将检查用户是否已返回该应用程序,但不会触发该应用程序被杀死的情况。
  • Ready will trigger when platform's ready, you'll use this to check if the app was killed and is starting for "the first time". 准备就绪会在平台准备就绪时触发,您将使用它来检查应用程序是否已被终止并“首次”启动。
  • You'll need MomentJS to manipulate time, since it's easier to add days/hours to your current datetime. 您将需要MomentJS来操纵时间,因为将天/小时添加到当前日期时间更加容易。
  • LocalNotification accepts both ISO string and UNIX timestamp to schedule a notification. LocalNotification接受ISO字符串和UNIX时间戳来安排通知。
  • Inside your created service you'll have methods to schedule and cancel a LocalNotification, check the plugin wiki for those. 在创建的服务中,您将拥有计划和取消LocalNotification的方法,请查看插件Wiki。
  • On the pause method you'll call the schedule method and schedule a localNotification having Moment add 1 day to your current time: Moment().add(1, 'day').unix() and use the result in the 'at' of your schedule. 在暂停方法上,您将调用schedule方法,并计划一个具有Moment的localNotification,将Moment添加到当前时间1天: Moment().add(1, 'day').unix()并在'at'中使用结果您的时间表。
  • This'll return a promise with the data of your notification and it's id, save this id in your localStorage like this.storage.set('Daily', ReturnedID); 这将返回一个带有您的通知数据及其ID的this.storage.set('Daily', ReturnedID); ,将此ID像this.storage.set('Daily', ReturnedID);一样保存在您的localStorage中this.storage.set('Daily', ReturnedID);
  • When the app is ready or resume you'll cancel the notification. 应用就绪或恢复后,您将取消通知。
  • So basically that's it, when going off you save a notification, when going in you cancel it. 基本上就是这样,关闭时保存通知,进入时取消通知。

The second scenario is a little bit harder and you'll need a back-end for this since you'll need to keep checking the time that user has gone off the app. 第二种情况要难一些,并且您需要一个后端,因为您需要不断检查用户退出应用程序的时间。 You'll need only MomentJS , the Platform import, you push service plugin and a way to connect to your database (via Http or plugin, if it's a service like Firebase). 您只需要MomentJSPlatform导入,推送服务插件以及一种连接到数据库的方式即可(通过Http或插件,如果它是Firebase之类的服务)。

  • The steps are the same for the first scenario, the new service, the platform calling this service methods, but now will add some methods for you push plugin, so when you receive a push you can show and manipulate data. 对于第一个场景,新服务,调用该服务方法的平台,步骤是相同的​​,但是现在将为您的push插件添加一些方法,因此当您收到push时,可以显示和操作数据。
  • Most push services (and localNotification) have a Data attribute where you can share some data with the user, in your case it'll be the data you want to safe in the localStorage. 大多数推送服务(和localNotification)都具有Data属性,您可以在其中与用户共享一些数据,在这种情况下,它将是您要在localStorage中保护的数据。
  • You'll need a token of that user so you can send a push to him, see your push docs to see how you can get that token and save it to the user database register/node. 您将需要该用户的令牌,以便可以向其发送推送,请查看您的推送文档,以了解如何获取该令牌并将其保存到用户数据库寄存器/节点。
  • When the user leave the app you'll store in your database the time you want the user to receive that push (i'm assuming you want to do the same as the first scenario, but in a different way). 当用户离开应用程序时,您希望用户收到该推送的时间存储在数据库中(我假设您要与第一种情况相同,但方式不同)。
  • When the user come back to the app, you'll detele that time register. 当用户返回到应用程序时,您将确定该时间寄存器。
  • The tricky part: you'll need your back-end to go through every user register and get the time they need to receive the push, so if the time is < the current time you'll send the push and delete that register. 棘手的部分:您将需要后端来检查每个用户寄存器,并获取他们需要接收推送的时间,因此,如果时间小于当前时间,则将发送推送并删除该寄存器。
  • In the app when the user receive the push a function'll save the data to localStorage, manipulate, ready or everything you want. 在应用程序中,当用户收到推送时,功能会将数据保存到localStorage,进行操作,准备就绪或您想要的所有内容。

Sorry i can't share some code, but if you need furter explanation or ideas just ask. 抱歉,我无法共享一些代码,但是如果您需要更多的解释或想法,请询问。 Hope this helps :D 希望这会有所帮助:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM