HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了IOS 10.2简单警报和徽章上的UserNotifications问题大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在写这篇文章之前,我做了很多关于UserNotification Framework的研究,它取代了 IOS 10中的UIlocalnotification.我也按照本教程学习了有关这个新功能的一切: http://useyourloaf.com/blog/local-notifications-with-ios-10/.

今天我遇到了很多麻烦来实现这些琐碎的通知,因为它是最近的新功能,我找不到任何解决方案(特别是在目标C中)!我目前有2个不同的通知,一个警报和一个徽章更新.

警报问题

在将我的手机从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后,没有任何理由没有任何显示,我没有改变任何东西,我看不出有什么遗漏……

徽章问题

我也试过(这次只在IOS 10.2中)在我的应用程序图标上实现了一个很好的标记,直到我试删除它.这是执行它的功能

+(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 %@: %@",error);
            }
        }];
    }
}

现在它没有增加徽章编号,因为它的名称应该建议,但它只是将徽章编号设置为1.文档说如果你将content.badge设置为0,它会删除它,但这不起作用.我尝试使用其他数字,当我手动将其更改为“2”,“3”等时……它会发生变化,但如果我将其设置为0,则无效.

另外,在我之前链接的教程中,它提到了几个函数作为getPendingNotificationrequests:completionHandler:和getDeliveredNotificationrequests:completionHandler:.我注意到,当我在调用incrementBadgeIcon之后立即调用这些函数时,如果content.badge设置为’1′,’2’等……它将出现在待处理通知列表中.但是,当我将其设置为0时,它不会出现在任何地方.我没有收到错误,Xcode中没有任何警告,我的应用程序徽章仍然存在.

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

预先感谢

PS:我也尝试使用removeAllPendingNotificationrequests和removeAllDeliveredNotifications两者都没有成功.

解决方法

关于警报:

当您的本地通知触发时,您的应用可能仍处于前台,因此您需要实施委托方法,以便通知执行任何操作.例如,在您的委托中定义此方法将允许通知显示警报,发出声音并更新徽章:

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

关于徽章:

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

我已针对此提交了radar.

作为解决方法,我发现在UNMutableNotificationContent对象上设置title属性,徽章值为NSnumber(值:0).如果缺少title属性,则不会触发.

添加title属性仍然不会向用户显示警告(更新:在iOS 11中不再是这种情况!),因此这是一种无需调用UIApplication对象即可将徽章值静更新为0的方法(通过UIApplication.shared.applicationIconBadgenumber = 0).

这是我的示例项目中的全部代码; ViewController代码中有@L_729_9@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,withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        print("Received notification: \(notification)")
        completionHandler([.alert,.sound])
    }
}

大佬总结

以上是大佬教程为你收集整理的IOS 10.2简单警报和徽章上的UserNotifications问题全部内容,希望文章能够帮你解决IOS 10.2简单警报和徽章上的UserNotifications问题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。