Edit: Die App in den Hintergrund zu stellen, hat den Trick getan.
Original:
Ich habe also versucht, eine Benachrichtigung zum neuen UNUserNotificationCenter hinzuzufügen, aber ich scheine es nicht zu bekommen.
Mein View Controller hat eine Aktion:
@IBAction func sendPressed(_ sender: AnyObject) {
let content = UNMutableNotificationContent()
content.title = "Hello"
content.body = "What up?"
content.sound = UNNotificationSound.default()
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)
// Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
print(error)
}
print("should have been added")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let center = UNUserNotificationCenter.current()
center.requestAuthorization([.alert, .sound]) { (granted, error) in
}
}
Und ich habe auch einen Notification Content Extension
im Projekt, aber es scheint überhaupt nicht ausgelöst zu sein, irgendwelche Ideen, was mir fehlt? Ich versuche das Beispiel aus der Benutzerdokumentation, aber es sagt mir nicht viel mehr oder ich habe es vermisst.
Hier: https://developer.Apple.com/reference/usernotifications/unmutablenotificationcontent
Außerdem: https://developer.Apple.com/reference/usernotificationsuihttps://developer.Apple.com/reference/usernotifications
Sie müssen sich für die Benachrichtigung anmelden ... Ich habe es versucht und das funktioniert.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let center = UNUserNotificationCenter.current()
center.requestAuthorization([.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
return true
}
Edit: Sie müssen Ihre App nicht in den Hintergrund stellen, um Benachrichtigungen ab iOS 10 anzuzeigen.
Verwenden Sie den folgenden Rückruf, um die Benachrichtigung so zu konfigurieren, dass sie im Vordergrund angezeigt wird.
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
Hier ist ein Beispielprojekt.
Mit Objective-C-Implementierung:
Ich habe hier ein Demo-Projekt geschrieben: iOS10AdaptationTips .
userNotifications importieren
///Notification become independent from Foundation
@import UserNotifications;
berechtigung für localNotification anfordern
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request authorization succeeded!");
[self showAlert];
}
}];
localNotification einplanen
anwendungssymbol-Ausweisnummer aktualisieren
// //Deliver the notification at 08:30 everyday
// NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
// dateComponents.hour = 8;
// dateComponents.minute = 30;
// UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES];
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"Elon said:" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"Hello Tom!Get up, let's play with Jerry!"
arguments:nil];
content.sound = [UNNotificationSound defaultSound];
/// 4. update application icon badge number
content.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
// Deliver the notification in five seconds.
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:5.f repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
content:content trigger:trigger];
/// 3. schedule localNotification
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"add NotificationRequest succeeded!");
}
}];
dann wird es so aussehen:
Im Hintergrund : Bildschirm sperren:
Bei Wiederholung standardmäßig nur eine anzeigen. Statt viele auf dem Sperrbildschirm unter iOS9 anzuzeigen: http://i67.tinypic.com/98t75s.jpg und unterstützen automatisch 3D Touch http://a67.tinypic.com/dorw3b.jpg
Ich schreibe hier eine Demo: iOS10AdaptationTips .
Hier sind ein paar Schritte:
Stellen Sie sicher, dass Sie über die Berechtigung verfügen. Wenn nicht, verwenden Sie UNUserNotificationCenter.current (). RequestAuthorization, um dies zu erhalten. Oder folgen Sie dem answer , wenn Sie möchten, dass die Anforderung mehrmals angezeigt wird.
Wenn Sie die Benachrichtigung foreground anzeigen möchten, müssen Sie UNUserNotificationCenterDelegate irgendwo zuweisen.
Zeig mir den Code
@IBAction func sendPressed(_ sender: AnyObject) {
let content = UNMutableNotificationContent()
content.title = "Hello"
content.body = "What up?"
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
print(error)
}
}
override func viewDidLoad(_ animated: Bool) {
super.viewDidLoad(animated)
// Assign the delegate
UNUserNotificationCenter.current().delegate = self
// Ask the permission
let center = UNUserNotificationCenter.current()
center.requestAuthorization([.alert, .sound]) { (granted, error) in
if granted {
// do something
}
}
}
// Remember to add UNUserNotificationCenterDelegate to your view controller
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("Got the msg...")
completionHandler([.badge, .sound, .alert])
}
Ich habe mein Problem wie folgt gelöst (Firebase, Swift 3):
Finden Sie diese Methode in Ihrem AppDelegate:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
Finde diese Zeile:
completionHandler()
Satz beenden:
completionHandler([.alert,.sound,.badge])
benachrichtigungen werden nicht ausgelöst, wenn Sie Ihre Präsentationsoptionen nicht an die completionHandler-Methode übergeben.
Ich habe eine Implementierung für Swift 3 gemacht, die helfen kann. Sie können sie hier überprüfen: https://stackoverflow.com/a/45381380/2296630