iPa*_*esh 6 core-data nsuserdefaults push-notification ios swift
我想构建一个应用程序,我可以接收推送通知并保存在客户端设备内以获得限制25通知.如果应用程序正在运行,可能无法运 如何存储PushNotification警报消息?如果运行该时间的应用程序到达存储在UserDefault中的通知警报消息,但是当应用程序处于后台或处于非活动状态时,该时间未在UserDefault中存储通知警报消息.我想知道我需要使用UserDefault或CoreData在客户端应用程序中存储推送通知消息吗?如果不是,我该怎么用?我真的需要一只手来接我.
请帮忙.谢谢.
步骤1:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
requestUserPermissions(application: application)
if let notification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: AnyObject]
{
let info : NSDictionary! = notification as NSDictionary
if info != nil
{
getDataFromNotification(userInfo: info as! [AnyHashable : Any])
Alert.showAlert((self.window?.rootViewController)!, message: "App is terminated", strtitle: "Notification")
}
}
return true
}
Run Code Online (Sandbox Code Playgroud)
第2步:
func requestUserPermissions(application: UIApplication)
{
if #available(iOS 10.0, *)
{
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options:[.badge, .alert, .sound])
{
(granted, error) in
if( !(error != nil) )
{
application.registerForRemoteNotifications()
}
// Enable or disable features based on authorization.
}
}
else {
// Fallback on earlier versions
if (!application.isRegisteredForRemoteNotifications)
{
application.applicationIconBadgeNumber = 0
application.registerForRemoteNotifications()
}
}
Run Code Online (Sandbox Code Playgroud)
步骤 3:
@objc(userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:) @available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
print("Userinfo1 \(response.notification.request.content.userInfo)")
getDataFromNotification(userInfo: response.notification.request.content.userInfo)
Alert.showAlert((self.window?.rootViewController)!, message: "I am in Background", strtitle: "Notification")
}
@objc(userNotificationCenter:willPresentNotification:withCompletionHandler:) @available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
print("Userinfo2 \(notification.request.content.userInfo)")
getDataFromNotification(userInfo: notification.request.content.userInfo)
Alert.showAlert((self.window?.rootViewController)!, message: "I am in forground", strtitle: "Notification")
}
Run Code Online (Sandbox Code Playgroud)
步骤4:
func getDataFromNotification(userInfo: [AnyHashable : Any])
{
let info : NSDictionary! = userInfo as NSDictionary
if info != nil
{
if let aps = info["aps"] as? NSDictionary
{
if let resultsDict = aps as? [String:AnyObject]
{
for _ in resultsDict
{
let str = dictCleanData.value(forKey: "alert")!
let time = dictCleanData.value(forKey: "day")!
let dict = ["msg" : str, "time": time]
UserDefaults.standard.set(dict, forKey: "dict")
UserDefaults.standard.synchronize()
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
第 5 步:检查其他视图控制器上保存的数据
override func viewDidLoad()
{
super.viewDidLoad()
if UserDefaults.standard.value(forKey: "dict") != nil
{
let dict : NSDictionary = UserDefaults.standard.value(forKey: "dict") as! NSDictionary
arr_Veges.add(dict)
if UserDefaults.standard.value(forKey: "arr_Veges") != nil
{
let arr = UserDefaults.standard.value(forKey: "arr_Veges") as! NSArray
for oldObj in arr
{
arr_Veges.add(oldObj)
}
}
UserDefaults.standard.set(arr_Veges, forKey: "arr_Veges")
UserDefaults.standard.synchronize()
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3509 次 |
| 最近记录: |