ios 開發之本地推送

來源:互聯網
上載者:User

標籤:

網路推送可能被人最為重視,但是本地推送有時候項目中也會運用到;

閑話少敘,代碼如下:

1、添加根視圖

self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];        self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];

2、本地建立一個button進行觸發

button = [UIButton buttonWithType:UIButtonTypeCustom];    button.frame = CGRectMake(20, 100, WIDTH - 40, 50);    button.backgroundColor = [UIColor blueColor];    [button setTitle:@"開始啦" forState:UIControlStateNormal];    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];    //Binder 方法    [button addTarget:self action:@selector(noticClick:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:button];

3、註冊一個通知

//設定本地通知 傳一個時間進去+ (void)registerLocalNotification:(NSInteger)alertTime{    UILocalNotification *notification = [[UILocalNotification alloc]init];    //設定觸發通知的時間    NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];    NSLog(@"fireDate=%@",fireDate);        notification.fireDate = fireDate;    //時區    notification.timeZone = [NSTimeZone defaultTimeZone];    //設定重複的間隔    notification.repeatInterval = kCFCalendarUnitSecond;        //通知內容    notification.alertBody = @"該起床了...";    notification.applicationIconBadgeNumber = 1;    //通知被觸發時播放的聲音    notification.soundName = UILocalNotificationDefaultSoundName;    //通知參數    NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"起床了,開始學習了ios開發了" forKey:@"key"];    notification.userInfo = userDict;        //ios8 以後,需要添加這個註冊,才能得到授權    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {        UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type                                                                                 categories:nil];        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];        // 通知重複提示的單位,可以是天、周、月        notification.repeatInterval = NSCalendarUnitDay;    } else {        // 通知重複提示的單位,可以是天、周、月        notification.repeatInterval = NSDayCalendarUnit;    }    //執行通知註冊    [[UIApplication sharedApplication]scheduleLocalNotification:notification];    }

4、調用這個方法

-(void)noticClick:(id)sender{    //調用通知    [ViewController registerLocalNotification:4];//4秒鐘後}

5、取消通知的方法

//取消某個本地推播通知+(void)cancelLocalNotificationWithKey:(NSString *)key{    //擷取所有本地通知數組    NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;        for (UILocalNotification *notification in localNotifications) {        NSDictionary *userInfo = notification.userInfo;        if (userInfo) {                        //根據設定通知參數時指定的key來擷取通知參數            NSString *info = userInfo[key];                        //如果找到需要取消的通知,則取消            if (info != nil) {                [[UIApplication sharedApplication]cancelLocalNotification:notification];                break;            }                    }    }}

6、調用這個方法

//本地通知回呼函數,當應用程式在前台時調用-(void)application:(UIApplication *)application didReceiveLocalNotification:(nonnull UILocalNotification *)notification{    NSLog(@"notif:%@",notification);        //這裡真是需要處理互動的地方    //擷取通知所帶的資料    NSString *notMes = [notification.userInfo objectForKey:@"key"];    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"本地通知(前台)" message:notMes delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];    [alertView show];        //更新顯示的角標個數    NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;    badge--;    badge = badge >= 0 ? badge : 0;    [UIApplication sharedApplication].applicationIconBadgeNumber = badge;        //在不許要再推送時,可以取消推送    [ViewController cancelLocalNotificationWithKey:@"key"];}

 

ios 開發之本地推送

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.