推送通知(Local Remote)

  • NSNotification是抽象的,不可見的

  • 推送通知是可見的(肉眼可見的)

  • 本地推送通知(Local Notification)

  • 遠程推送通知(Remote Notification)

  • 可以讓不再前臺運行的app,告知用戶app內部發生了什么事情
    在屏幕頂部顯示一塊橫幅(顯示具體內容)
    在屏幕中間彈出一個UIAlertView(顯示具體內容)
    在鎖屏界面顯示一塊橫幅(鎖屏狀態下,顯示具體內容)
    更新app圖標的數字(說明新內容的數量)
    播放音效(提醒作用)

注冊推送通知(本地)

  • 本地推送通知:不需要聯網就能發出推送通知(不需要服務器的支持)
  • 使用場景:常用來定時提醒用戶完成一些任務(清理垃圾 記賬 看電影 玩游戲)
#pragma mark - 在AppDelegate中注冊用戶通知權限
/*
 UIUserNotificationTypeBadge:圖標標記
 UIUserNotificationTypeSound:聲音
 UIUserNotificationTypeAlert:彈窗
 */
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //設置通知的內容和信息
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
    //注冊用戶權限
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    
    return YES;
}
#pragma mark - 創建通知 設置屬性(內容,聲音等) 將通知添加到調度池
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{  
    //創建本地推送通知
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    
    //設置發送通知的時間
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    //發送通知的消息體
    localNotification.alertBody = @"發送通知的消息體";
    //發送通知的聲音
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    
    //發送本地推送通知 -> 加入通知的調度池
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
#pragma mark - 推送通知的屬性
    //在消息體上添加一行內容
    localNotification.alertTitle = @"標題";
    //iOS10沒有效果 iOS10之前在鎖屏時顯示 滑動以查看后面文字
    localNotification.alertAction = @"鎖屏時調用";
    //圖標標記的數字 默認值就是0,如果是0,表示沒有改變
    localNotification.applicationIconBadgeNumber = 10;

#pragma mark - 本地推送通知攜帶信息
//本地推送通知攜帶的信息
    localNotification.userInfo = @{@"content":@"hehe",@"key":@(2)};
//關閉APP的情況下
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 //判斷是否有本地通知
    UILocalNotification *localNotification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
    if(localNotification){
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
        label.backgroundColor = [UIColor blueColor];
        label.text = [NSString stringWithFormat:@"%@",localNotification.userInfo];
        label.numberOfLines = 0;
        [self.window.rootViewController.view addSubview:label];
    }
}
//不關閉APP的情況下
//接收到本地通知時調用 ->點擊通知,要打開App時調用
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    NSLog(@"%@",notification.userInfo);
   //調試:控制臺 UI調試
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
    label.backgroundColor = [UIColor blueColor];
    label.text = [NSString stringWithFormat:@"%@",notification.userInfo];
    label.numberOfLines = 0;
    [self.window.rootViewController.view addSubview:label];
}
#pragma mark - 通知實現頁面跳轉通過攜帶信息userInfo
- (IBAction)page1:(id)sender {
    [self postLocalNotificationWithAlertBody:@"page1" andUserInfo:@{@"content":@"session",@"key":@(1)}];
}
//將發送通知的方法抽取出來
-(void)postLocalNotificationWithAlertBody:(NSString *)alertBody andUserInfo:(NSDictionary *)userInfo {
    //創建本地推送通知
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    
    //設置發送通知的時間
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    //發送通知的消息體
    localNotification.alertBody = alertBody;
    //發送通知的聲音
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    
    
    //本地推送通知攜帶的信息
    localNotification.userInfo = userInfo;

    //發送本地推送通知 -> 加入通知的調度池
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
if([UIApplication sharedApplication].applicationState == UIApplicationStateActive){
        //程序在前臺,不需要跳轉
        return;
    }
//-----------------------------------------------------------------------------------
//根據localNotification的信息跳轉到不同的頁面
-(void)jumpToViewWith:(UILocalNotification *)localNotification {
    //獲取通知中的詳細信息
    NSDictionary *userInfo = localNotification.userInfo;
    //獲取key的值
    NSUInteger index = [userInfo[@"key"] unsignedIntegerValue];
    //獲取tabbat控制器
    UITabBarController *tabVC = (UITabBarController *)self.window.rootViewController;
    //跳轉控制器
    tabVC.selectedIndex = index;
}
#pragma mark - 注冊通知的categories
//設置分類
    localNotification.category = @"category";
//-----------------------------------------------------------------------------------
//iOS8之后添加分類功能
    UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
    //設置標識符
    category.identifier = @"category";
    //創建按鈕
    UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
    //按鈕的標識符
    action.identifier = @"foreground";
    //按鈕的標題
    action.title = @"打開應用";
    //不打開手機 UIUserNotificationActivationModeBackground
    //打開手機 UIUserNotificationActivationModeForeground
    action.activationMode = UIUserNotificationActivationModeForeground;
    
    //創建按鈕1
    UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
    //按鈕的標識符
    action1.identifier = @"background";
    //按鈕的標題
    action1.title = @"稍后處理";
    //不打開手機 UIUserNotificationActivationModeBackground
    //打開手機 UIUserNotificationActivationModeForeground
    action1.activationMode = UIUserNotificationActivationModeBackground;
    
    //添加按鈕
    [category setActions:@[action,action1] forContext:UIUserNotificationActionContextDefault];
    
    //設置通知的內容和信息
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:[NSSet setWithObject:category]];
#pragma mark - 點擊推送通知的按鈕時會調用該方法
//點按通知中的按鈕時會調用
//identifier :按鈕的標識符
//notification:本地推送通知
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler{
    if([identifier isEqualToString:@"foreground"]){
        NSLog(@"打開了應用");
    }else if([identifier isEqualToString:@"background"]){
        NSLog(@"稍后處理");
    }
    //讓蘋果預估方法運行時間 告訴蘋果方法執行完畢
    completionHandler();
}
#pragma mark - 添加輸入框
//iOS9增加方法 添加輸入框
    action.behavior = UIUserNotificationActionBehaviorTextInput;
//-------------------------------------------------------------
//點按通知中的按鈕時會調用
//identifier :按鈕的標識符
//notification:本地推送通知
//responseInfo:用戶在輸入框中輸入的文字
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler{
    //獲取用戶輸入的文字
    NSString *str = responseInfo[UIUserNotificationActionResponseTypedTextKey];
    //將文字發送給服務器 實現立即回復
    NSLog(@"%@",str); 
    //讓蘋果預估方法運行時間 告訴蘋果方法執行完畢
    completionHandler();
}

遠程推送

  • 從服務器推送給客戶端的通知(需要聯網)

  • 遠程推送服務,又稱APNs(Apple Push Notification Services)

  • 不管用戶打開還是關閉app,只要聯網,都能收到服務器推送的遠程通知

  • 所有的蘋果設備,在聯網的狀態下,都會與蘋果的服務器建立長連接

  • UDID手機唯一標識+bundleID應用程序唯一標識 -> deviceToken

遠程推送實現

遠程推送實現.png
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //1.請求用戶授權
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    
    //2.獲取deviceToken  向蘋果發送服務
    [[UIApplication sharedApplication] registerForRemoteNotifications];
    
    return YES;
}
//當蘋果加密成功后,返回deviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    NSLog(@"%@",deviceToken);
}
//當蘋果加密失敗,返回原因
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"%@",error);
}
//寫在didFinishLaunchingWithOptions中,程序被殺死時遠程推送
if(launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]){
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
        label.text = [NSString stringWithFormat:@"%@",launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
        label.numberOfLines = 0;
        [self.window.rootViewController.view addSubview:label];
    
//接收到了遠程推送通知 程序在前臺和后臺運行時能夠接受
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    NSLog(@"%@",userInfo);
}

極光推送

極光推送-1.png
極光推送-2.png
極光推送-3.png
導出證書.png
極光推送-4.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 推送通知注意:這里說的推送通知跟NSNotification有所區別NSNotification是抽象的,不可見的...
    醉葉惜秋閱讀 1,536評論 0 3
  • 極光推送: 1.JPush當前版本是1.8.2,其SDK的開發除了正常的功能完善和擴展外也緊隨蘋果官方的步伐,SD...
    Isspace閱讀 6,770評論 10 16
  • 前言 本文是一篇轉載文章,在這一篇實用的文章里,你可以按照上面的步驟實現不借助第三方和服務器端,自己給自己的設備發...
    進無盡閱讀 1,694評論 6 6
  • 一、推送通知 注意:這里說的推送通知跟NSNotification有所區別NSNotification是抽象的,不...
    Mg明明就是你閱讀 1,267評論 0 17
  • 推送通知 注意:這里說的推送通知跟NSNotification有所區別 NSNotification是抽象的,不可...
    iOS開發攻城獅閱讀 4,303評論 1 13