iOS APP 在前台時彈出本地通知


 iOS10 之后使用才有效果

 1.在 AppDelegate.m 文件里面添加下面的方法。

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    
    completionHandler(UNNotificationPresentationOptionBadge
                      |UNNotificationPresentationOptionSound
                      |UNNotificationPresentationOptionAlert);
}

 

2. 對content添加屬性的方式實現。

               UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
                content.body = @"本地通知--content";
                content.title = @"本地通知--title";
                content.sound = [UNNotificationSound defaultSound];
                content.badge = @(msgArr.count);
                //10之后的系統可以設置在前台顯示
                [content setValue:@(YES) forKeyPath:@"shouldAlwaysAlertWhileAppIsForeground"];
                //  觸發模式
                UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO];

                // 創建本地通知
                NSString *requestIdentifer = [NSString stringWithFormat:@"TestRequestww1%u",arc4random() % 1000];
                UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifer content:content trigger:trigger];

                //把通知加到UNUserNotificationCenter, 到指定觸發點會被觸發
                [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {

                }];

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM