gpt4 book ai didi

IOS 10.2 UserNotifications 关于简单警报和角标(Badge)的问题

转载 作者:可可西里 更新时间:2023-11-01 03:29:52 27 4
gpt4 key购买 nike

在写这篇文章之前,我对 UserNotification Framework 进行了大量研究,它取代了 IOS 10 中的 UILocalNotification。我还按照本教程学习了有关此新功能的所有信息:http://useyourloaf.com/blog/local-notifications-with-ios-10/ .

今天我在实现如此微不足道的通知时遇到了很多麻烦,而且由于这是最近的一项新功能,我找不到任何解决方案(尤其是在 Objective-C 中)!我目前有 2 种不同的通知,一种是警报,另一种是角标(Badge)更新

警报问题

在将我的手机从 IOS 10.1 更新到 10.2 之前,我在 Appdelegate 上发出了一个警报,只要用户手动关闭应用程序就会立即触发:

-(void)applicationWillTerminate:(UIApplication *)application {
NSLog(@"applicationWillTerminate");

// Notification terminate
[self registerTerminateNotification];
}

// Notification Background terminate
-(void) registerTerminateNotification {
// the center
UNUserNotificationCenter * notifCenter = [UNUserNotificationCenter currentNotificationCenter];

// Content
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = @"Stop";
content.body = @"Application closed";
content.sound = [UNNotificationSound defaultSound];
// Trigger
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO];
// Identifier
NSString *identifier = @"LocalNotificationTerminate";
// création de la requête
UNNotificationRequest *terminateRequest = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
// Ajout de la requête au center
[notifCenter addNotificationRequest:terminateRequest withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Error %@: %@",identifier,error);
}
}];
}

在 IOS 10.2 之前它工作得很好,当我手动关闭应用程序时,会出现一个警告。但是自从我更新到IOS 10.2之后,无缘无故什么都没有出现,我什么都没改,也看不出少了什么..

角标(Badge)问题

我也尝试(这次只在 IOS 10.2 中)在我的应用程序图标上实现角标(Badge),效果很好,直到我试图删除它。这是执行此操作的函数:

+(void) incrementBadgeIcon {
// only increment if application is in background
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground){

NSLog(@"increment badge");

// notif center
UNUserNotificationCenter *notifCenter = [UNUserNotificationCenter currentNotificationCenter];

// Content
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.badge = [NSNumber numberWithInt:1];
// Trigger
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO];
// Identifier
NSString *identifier = @"LocalNotificationIncrementBadge";
// request
UNNotificationRequest *incrementBadgeRequest = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
// Ajout de la requête au center
[notifCenter addNotificationRequest:incrementBadgeRequest withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Error %@: %@",identifier,error);
}
}];
}
}

现在它不会增加角标(Badge)编号,正如它的名字应该暗示的那样,但它只是将角标(Badge)编号设置为 1。文档说如果您将 content.badge 设置为 0,它会删除它,但这不起作用。我尝试使用其他数字,当我手动将其更改为“2”、“3”等时……它发生了变化,但如果我将其设置为 0,则不起作用。

此外,在我之前链接的教程中,提到了几个函数,如 getPendingNotificationRequests:completionHandler:getDeliveredNotificationRequests:completionHandler:。我注意到,当我在调用 incrementBadgeIcon 后立即调用这些函数时,如果 content.badge 设置为“1”、“2”等...它会出现在待处理通知列表中。但是,当我将它设置为 0 时,它不会出现在任何地方。我在 Xcode 中没有收到任何错误和警告,而且我的应用程序角标(Badge)仍然存在。

有谁知道如何修复这两个警报?

提前致谢

PS:我也尝试过使用 removeAllPendingNotificationRequestsremoveAllDeliveredNotifications 但都没有成功。

最佳答案

关于警报:

当您的本地通知触发时,您的应用程序可能仍在前台运行,因此您需要实现委托(delegate)方法以便通知执行任何操作。例如,在您的委托(delegate)中定义此方法将允许通知显示警报、发出声音并更新角标(Badge):

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert,.badge,.sound])
}

关于角标(Badge):

我观察到创建一个 UNMutableNotificationContent 对象并仅指定角标(Badge)值(作为 NSNumber 对象)适用于除 0 以外的所有角标(Badge)值(即,您不能以这种方式清除角标(Badge))。我还没有找到任何文档来说明为什么 0 的行为与任何其他值不同,特别是因为 .badge 属性被定义为 NSNumber?,所以框架应该能够区分 nil(无变化)和 0(清除角标(Badge))。

我已经提交了 radar反对这一点。

作为变通方法,我发现在 UNMutableNotificationContent 对象上设置 title 属性,角标(Badge)值为 NSNumber(value: 0) 触发。如果缺少 title 属性,则不会触发。

添加 title 属性仍然不会向用户显示警报(更新:iOS 11 不再是这种情况!),因此这是一种静默更新角标(Badge)值的方法0 而无需调用 UIApplication 对象(通过 UIApplication.shared.applicationIconBadgeNumber = 0)。

这是我示例项目中的全部代码; ViewController 代码中有一个 MARK,显示在何处插入 title 属性可以解决问题:

//
// AppDelegate.swift
// userNotificationZeroBadgeTest
//
// Created by Jeff Vautin on 1/3/17.
// Copyright © 2017 Jeff Vautin. All rights reserved.
//

import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert, .sound]) { (success, error) -> Void in
print("Badge auth: \(success)")
}

// For handling Foreground notifications, this needs to be assigned before finishing this method
let vc = window?.rootViewController as! ViewController
let center = UNUserNotificationCenter.current()
center.delegate = vc

return true
}
}

//
// ViewController.swift
// userNotificationZeroBadgeTest
//
// Created by Jeff Vautin on 1/3/17.
// Copyright © 2017 Jeff Vautin. All rights reserved.
//

import UIKit
import UserNotifications

class ViewController: UIViewController, UNUserNotificationCenterDelegate {

@IBAction func start(_ sender: Any) {
// Reset badge directly (this always works)
UIApplication.shared.applicationIconBadgeNumber = 0


let center = UNUserNotificationCenter.current()

// Schedule badge value of 1 in 5 seconds
let notificationBadgeOneContent = UNMutableNotificationContent()
notificationBadgeOneContent.badge = NSNumber(value: 1)
let notificationBadgeOneTrigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 1*5, repeats: false)
let notificationBadgeOneRequest = UNNotificationRequest.init(identifier: "1", content: notificationBadgeOneContent, trigger: notificationBadgeOneTrigger)
center.add(notificationBadgeOneRequest)

// Schedule badge value of 2 in 10 seconds
let notificationBadgeTwoContent = UNMutableNotificationContent()
notificationBadgeTwoContent.badge = NSNumber(value: 2)
let notificationBadgeTwoTrigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 2*5, repeats: false)
let notificationBadgeTwoRequest = UNNotificationRequest.init(identifier: "2", content: notificationBadgeTwoContent, trigger: notificationBadgeTwoTrigger)
center.add(notificationBadgeTwoRequest)

// Schedule badge value of 3 in 15 seconds
let notificationBadgeThreeContent = UNMutableNotificationContent()
notificationBadgeThreeContent.badge = NSNumber(value: 3)
let notificationBadgeThreeTrigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 3*5, repeats: false)
let notificationBadgeThreeRequest = UNNotificationRequest.init(identifier: "3", content: notificationBadgeThreeContent, trigger: notificationBadgeThreeTrigger)
center.add(notificationBadgeThreeRequest)

// Schedule badge value of 4 in 20 seconds
let notificationBadgeFourContent = UNMutableNotificationContent()
notificationBadgeFourContent.badge = NSNumber(value: 4)
let notificationBadgeFourTrigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 4*5, repeats: false)
let notificationBadgeFourRequest = UNNotificationRequest.init(identifier: "4", content: notificationBadgeFourContent, trigger: notificationBadgeFourTrigger)
center.add(notificationBadgeFourRequest)

// Schedule badge value of 0 in 25 seconds
let notificationBadgeZeroContent = UNMutableNotificationContent()
// MARK: Uncommenting this line setting title property will cause notification to fire properly.
//notificationBadgeZeroContent.title = "Zero!"
notificationBadgeZeroContent.badge = NSNumber(value: 0)
let notificationBadgeZeroTrigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5*5, repeats: false)
let notificationBadgeZeroRequest = UNNotificationRequest.init(identifier: "0", content: notificationBadgeZeroContent, trigger: notificationBadgeZeroTrigger)
center.add(notificationBadgeZeroRequest)
}

@IBAction func listNotifications(_ sender: Any) {
let center = UNUserNotificationCenter.current()
center.getDeliveredNotifications() { (notificationArray) -> Void in
print("Delivered notifications: \(notificationArray)")
}
center.getPendingNotificationRequests() { (notificationArray) -> Void in
print("Pending notifications: \(notificationArray)")
}
}

// MARK: UNUserNotificationCenterDelegate

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("Received notification: \(notification)")
completionHandler([.alert,.badge,.sound])
}
}

关于IOS 10.2 UserNotifications 关于简单警报和角标(Badge)的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41210373/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com