iOSローカルメッセージ転送機構
9990 ワード
転送先:https://www.jianshu.com/p/e347f999ed95 //もう廃れました
http://blog.csdn.net/three_zhang/articale/detail/70170215
http://www.cocoachina.com/ios/20160926/17645.html //詳解編
https://www.jianshu.com/p/c58f8322a278 //詳解編
iOS 10では、これまでの
iOS 10の前
登録のお知らせ
通知を送る
ここで言うと、iOSシステムは登録ローカルプッシュ数を制限し、最大の登録量は64条である。
プッシュを受けるは、フロントまたはバックグラウンドに適用され、殺されていない場合。
プログラムが殺された時
iOS 10の後
まずこれを導入します。
登録のお知らせ
通知を送る
通知処理最初の方法:
この方法の中のその言葉は、フロントに適用されると、現地の通知を受けて、どのような方式で表現されますか?システムは三つの形式を与えました。
第二のプロキシ方法: この方法はバックグラウンドやプログラムが殺された時、通知欄をクリックして呼び出します。フロントでは呼び出しられません。
http://blog.csdn.net/three_zhang/articale/detail/70170215
http://www.cocoachina.com/ios/20160926/17645.html //詳解編
https://www.jianshu.com/p/c58f8322a278 //詳解編
iOS 10では、これまでの
UILocalNotification
が破棄され、新しいUserNotifications Framework
が採用されています。とりあえずiOS 10の前のローカルプッシュフローを教えてください。iOS 10の前
登録のお知らせ
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if ([[UIDevice currentDevice].systemVersion floatValue] > 8.0) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; [application registerUserNotificationSettings:settings]; } return YES; }
通知を送る
// 1.
UILocalNotification *localNote = [[UILocalNotification alloc] init];
// 1.1.
localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:5]; // 1.2. localNote.alertBody = @" "; // 1.3. , localNote.alertAction = @" "; localNote.hasAction = YES; // 1.4. ( ) localNote.alertLaunchImage = @"../Documents/1.jpg"; // 1.5. localNote.soundName = UILocalNotificationDefaultSoundName; // 1.6. localNote.applicationIconBadgeNumber = 1; // 1.7. localNote.userInfo = @{@"hello" : @"how are you", @"msg" : @"success"}; // 2. [[UIApplication sharedApplication] scheduleLocalNotification:localNote];
ここで言うと、iOSシステムは登録ローカルプッシュ数を制限し、最大の登録量は64条である。
プッシュを受ける
//
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
NSLog(@"333 "); UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" message:@" , , " delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil]; [alert show]; }
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) { // NSLog(@"666 "); UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" message:@" , " delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil]; [alert show]; } return YES; }
iOS 10の後
まずこれを導入します。
登録のお知らせ
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// UNUserNotificationCenter UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; // center.delegate = self; //iOS 10 , [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) { // Enable or disable features based on authorization. }]; return YES; }
通知を送る
// UNUserNotificationCenter
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
// UNMutableNotificationContent , UNNotificationContent , 。
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@" Title" arguments:nil]; content.body = [NSString localizedUserNotificationStringForKey:@" Body" arguments:nil]; content.sound = [UNNotificationSound defaultSound]; // UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO]; UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond" content:content trigger:trigger]; // ! [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { }];
通知処理
#import
プロキシ方法を実装する:-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{ // completionHandler , completionHandler(UNNotificationPresentationOptionSound); }
この方法の中のその言葉は、フロントに適用されると、現地の通知を受けて、どのような方式で表現されますか?システムは三つの形式を与えました。
typedef NS_OPTIONS(NSUInteger, UNNotificationPresentationOptions) {
UNNotificationPresentationOptionBadge = (1 << 0),
UNNotificationPresentationOptionSound = (1 << 1),
UNNotificationPresentationOptionAlert = (1 << 2),
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil]; [alert show]; completionHandler(); }
転載先:https://www.cnblogs.com/CodingMann/p/8183578.html