iOS 本地推送通知
// // AppDelegate.m // ClockTest // // Created by Chocolate. on 14-3-26. // Copyright (c) 2014年 redasen. All rights reserved. // #import "AppDelegate.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //一种方式 UILocalNotification *notification=[[UILocalNotification alloc] init]; if (notification!=nil) { NSLog(@">> support local notification"); NSDate *now=[NSDate new]; notification.fireDate=[now addTimeInterval:10]; notification.soundName= UILocalNotificationDefaultSoundName;//声音,可以换成alarm.soundName = @"myMusic.caf" notification.timeZone=[NSTimeZone defaultTimeZone]; notification.alertBody=@"该去吃晚饭了!"; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; } //一种方式 NSDictionary *myDic = [NSDictionary dictionaryWithObject:@"apple"forKey:@"a"]; NSString *myString = @"my body"; [self creatLocalNotification:5.0 timeZone:[NSTimeZone systemTimeZone] userInfor:myDic alertBody:myString]; [self creatLocalNotification:20.0 timeZone:[NSTimeZone systemTimeZone] userInfor:myDic alertBody:myString]; return YES; } -(void)creatLocalNotification:(NSTimeInterval)timeInterval timeZone:(NSTimeZone*)zone userInfor:(NSDictionary*)userDic alertBody:(NSString*)body { UILocalNotification *notification=[[UILocalNotification alloc] init];//新建通知 notification.soundName= UILocalNotificationDefaultSoundName;//声音,可以换成alarm.soundName = @"myMusic.caf" notification.fireDate=[[NSDate date] dateByAddingTimeInterval:timeInterval];//距现在多久后触发代理方法 notification.timeZone=zone;//设置时区 notification.userInfo=userDic;//在字典用存需要的信息 //去掉下面2行就不会弹出提示框 notification.alertBody=body;//提示信息弹出提示框 notification.alertAction = @"打开"; //提示框按钮 // notification.hasAction = NO; //是否显示额外的按钮,为no时alertAction消失 [[UIApplication sharedApplication] scheduleLocalNotification:notification];//将新建的消息加到应用消息队列中 // NSArray*arrSchedule=[[UIApplication sharedApplication]scheduledLocalNotifications];//获得所有已注册但未到时提醒的本地消息 // 3.消息接受 // - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification // 这个方法是消息到时间后进此代理,在这里可以设置他弹框或者别的(你想做的操作) // **注意如果在此程序在后台运行时将不会听你的话。可以在系统设置中进行消息通知设置是让他弹框还是横条提醒随你设置。此时已提醒的消息会显示在手机的消息通知栏中(只要你不手动将此消息取消则他会一直显示在消息通知栏中) // 其中有一点很重要UIApplicationState state = application.applicationState // 这个状态分为UIApplicationStateActive(活动在前台)UIApplicationStateBackground(程序进入后台)UIApplicationStateInactive(进入消息通知栏时点击某条消息) // 4.取消消息 // - (void)cancelLocalNotification:(UILocalNotification *)notification;//取消一条消息 // - (void)cancelAllLocalNotifications;//取消所有已发出的消息(此时消息栏中不会有消息提醒) } - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ //如果想让手机运行着这个项目的情况下,必须在下面写弹出框。并且是在打开。如果在后台,也不用。 } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。