登记本地文告,SWIFT推送之本地推送

SWIFT推送之当地推送(UILocalNotification)

本地推送公告是经超过实际例化UILocalNotification达成的。要促开销地化推送能够在AppDelegate.swift中增添代码完毕,技能例是三个当App进入后台时推送一条新闻给用户。

1.第3在didFinishLaunchingWithOptions方法内增加代码,IOS8推送新闻首先要拿走用户的允许,在初次安装App时会提示用户是或不是允许程序推送消息,此办法是App第二回运营的时候被试行1次,每趟从后台激活时不实践该方法.

func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)
-> Bool {

        if (UIDevice.currentDevice().systemVersion as
NSString).floatValue >= 8 {

           
//APService.registerForRemoteNotificationTypes(

                //UIUserNotificationType.Badge.rawValue
|

                //UIUserNotificationType.Sound.rawValue
|

               
//UIUserNotificationType.Alert.rawValue,

                //categories: nil)

            

           
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes:

                UIUserNotificationType.Badge |

                UIUserNotificationType.Sound |

                UIUserNotificationType.Alert,
categories: nil))

        

        }

        

        APService.setupWithOption(launchOptions)

        return true

    }

登记本地文告,SWIFT推送之本地推送。 二.有多少个艺术要说一下,

   一.func application威尔ResignActive(application:
UIApplication){}
当App既将跻身后台、锁屏、有电话进来时会触发此事件

   贰.func applicationDidEnterBackground(application:
UIApplication) {} 当App进入后台时触发此事件

   叁.func applicationWillEnterForeground(application:
UIApplication) {} 当App从后台就要重返前台时触发此事件

   四.func applicationDidBecomeActive(application:
UIApplication) {}当App产生活动状态时触发此事件

   五.func application威尔Terminate(application:
UIApplication) {}
当App退出时触发此方法,一般用于保存有个别特定的数码

那时在applicationDidEnterBackground方法内写入以下代码:

func applicationDidEnterBackground(application:
UIApplication) {

        // 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.

       
UIApplication.sharedApplication().cancelAllLocalNotifications()

        

        var notification = UILocalNotification()

        //notification.fireDate =
NSDate().dateByAddingTimeInterval(1)

        //setting timeZone as localTimeZone

        notification.timeZone =
NSTimeZone.localTimeZone()

        notification.repeatInterval =
NSCalendarUnit.CalendarUnitDay

        notification.alertTitle = “This is a local
notification”

        notification.alertBody = “Hey,It‘s great to see
you again”

        notification.alertAction = “OK”

        notification.soundName =
UILocalNotificationDefaultSoundName

        //setting app‘s icon badge

        notification.applicationIconBadgeNumber =
1

        

        var userInfo:[NSObject : AnyObject] =
[NSObject : AnyObject]()

        userInfo[“kLocalNotificationID”] =
“LocalNotificationID”

        userInfo[“key”] = “Attention Please”

        notification.userInfo = userInfo

        

       
//UIApplication.sharedApplication().scheduleLocalNotification(notification)

       
//UIApplication.sharedApplication().presentLocalNotificationNow(notification)

       
application.presentLocalNotificationNow(notification)

    }

 此时将按Home键将App切换来后台时会有一条推送音信,App角标变为了“1”

 

 

叁.当用户点击消息时会触发didReceiveLocalNotification事件,在这一个事件内写些代码:

func application(application: UIApplication,
didReceiveLocalNotification notification: UILocalNotification) {

       
UIApplication.sharedApplication().cancelAllLocalNotifications()

        let userInfo = notification.userInfo!

        let title = userInfo[“key”] as! String

        

        var alert = UIAlertView()

        alert.title = title

        alert.message = notification.alertBody

       
alert.addButtonWithTitle(notification.alertAction!)

        alert.cancelButtonIndex = 0

        alert.show()

            

       
//APService.showLocalNotificationAtFront(notification, identifierKey:
nil)

    }

 

四.超越后处于活动状态的时候解除ICON的角标

func applicationDidBecomeActive(application:
UIApplication) {

        // 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.

        //setting the desk top application icon‘s badge
as zero

        //application.applicationIconBadgeNumber =
0

       
application.cancelAllLocalNotifications()

        application.applicationIconBadgeNumber =
0

        

    }

地面推送布告是因而实例化UILocalNotification落成的。要达成本地化推送能够在AppDelegate.swift中增添代码落成,才具例是三个当App进入后台时推送一条新闻给用户。

SWIFT推送之本地推送(UILocalNotification)

本地推送通告是透超过实际例化UILocalNotification完结的。要促费用地化推送能够在AppDelegate.swift中加多代码达成,才具例是3个当App进入后台时推送一条信息给用户。

一.率先在didFinishLaunchingWithOptions方法内增多代码,IOS八推送音信首先要得到用户的允许,在初次安装App时会提示用户是还是不是同意程序推送音信,此措施是App第1回运维的时候被实行3遍,每回从后台激活时不进行该方法.

func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)
-> Bool {

        if (UIDevice.currentDevice().systemVersion as
NSString).floatValue >= 8 {

           
//APService.registerForRemoteNotificationTypes(

                //UIUserNotificationType.Badge.rawValue
|

                //UIUserNotificationType.Sound.rawValue
|

               
//UIUserNotificationType.Alert.rawValue,

                //categories: nil)

            

           
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes:

                UIUserNotificationType.Badge |

                UIUserNotificationType.Sound |

                UIUserNotificationType.Alert,
categories: nil))

        

        }

        

        APService.setupWithOption(launchOptions)

        return true

    }

 二.有多少个点子要说一下,

   一.func application威尔ResignActive(application:
UIApplication){}
当App既将进入后台、锁屏、有电话进来时会触发此事件

   2.func applicationDidEnterBackground(application:
UIApplication) {} 当App进入后台时触发此事件

   3.func application威尔EnterForeground(application:
UIApplication) {} 当App从后台将要重回前台时触发此事件

   肆.func applicationDidBecomeActive(application:
UIApplication) {}当App产生活动状态时触发此事件

   伍.func application威尔Terminate(application:
UIApplication) {}
当App退出时触发此方法,一般用来保存有个别特定的数量

这会儿在applicationDidEnterBackground方法内写入以下代码:

func applicationDidEnterBackground(application:
UIApplication) {

        // 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.

       
UIApplication.sharedApplication().cancelAllLocalNotifications()

        

        var notification = UILocalNotification()

        //notification.fireDate =
NSDate().dateByAddingTimeInterval(1)

        //setting timeZone as localTimeZone

        notification.timeZone =
NSTimeZone.localTimeZone()

        notification.repeatInterval =
NSCalendarUnit.CalendarUnitDay

        notification.alertTitle = “This is a local
notification”

        notification.alertBody = “Hey,It‘s great to see
you again”

        notification.alertAction = “OK”

        notification.soundName =
UILocalNotificationDefaultSoundName

        //setting app‘s icon badge

        notification.applicationIconBadgeNumber =
1

        

        var userInfo:[NSObject : AnyObject] =
[NSObject : AnyObject]()

        userInfo[“kLocalNotificationID”] =
“LocalNotificationID”

        userInfo[“key”] = “Attention Please”

        notification.userInfo = userInfo

        

       
//UIApplication.sharedApplication().scheduleLocalNotification(notification)

       
//UIApplication.sharedApplication().presentLocalNotificationNow(notification)

       
application.presentLocalNotificationNow(notification)

    }

 此时将按Home键将App切换来后台时会有一条推送新闻,App角标变为了“一”

 

 

三.当用户点击新闻时会触发didReceiveLocalNotification事件,在那个事件内写些代码:

func application(application: UIApplication,
didReceiveLocalNotification notification: UILocalNotification) {

       
UIApplication.sharedApplication().cancelAllLocalNotifications()

        let userInfo = notification.userInfo!

        let title = userInfo[“key”] as! String

        

        var alert = UIAlertView()

        alert.title = title

        alert.message = notification.alertBody

       
alert.addButtonWithTitle(notification.alertAction!)

        alert.cancelButtonIndex = 0

        alert.show()

            

       
//APService.showLocalNotificationAtFront(notification, identifierKey:
nil)

    }

 

4.当主次处于活动状态的时候解除ICON的角标

func applicationDidBecomeActive(application:
UIApplication) {

        // 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.

        //setting the desk top application icon‘s badge
as zero

        //application.applicationIconBadgeNumber =
0

       
application.cancelAllLocalNotifications()

        application.applicationIconBadgeNumber =
0

        

    }

iOS 注册本地布告(推送)

注:按Home键让App进入后台运维时,方可查看文告。

 

 

– (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

 

// 注册本地通告

UILocalNotification *localnotifit = [[UILocalNotification alloc]
init];

if (localnotifit) {

 

// 获取文告时间

NSDate *now = [NSDate date];

localnotifit.timeZone = [NSTimeZone defaultTimeZone];

 

// 10秒后初叶布告

localnotifit.fireDate = [now dateByAddingTimeInterval:10.0];

 

// 重复间隔 (下限为每分钟文告二回)

localnotifit.repeatInterval = kCFCalendarUnitMinute;

 

// 提示内容

localnotifit.alertBody = @拾秒后手提式有线电话机将会放炮,急忙扔掉;

 

//
锁屏状态下,“滑动来”(那八个字是系统自动出现的)后边跟着文字正是alertAction

localnotifit.alertAction = @解锁(进入App);

 

// 公告栏里的照顾标题

localnotifit.alertTitle = @提示;

 

// 暗许的打招呼声音(唯有在真机上才会听到)

localnotifit.soundName = UILocalNotificationDefaultSoundName;

 

// 白色圈圈数字

localnotifit.applicationIconBadgeNumber = 1;

 

// 公告标记

NSMutableDictionary *dic = [NSMutableDictionary
dictionaryWithObject:@LocalNotificationID forKey:@key];

localnotifit.userInfo = dic;

 

// 将布告加多到系统中

[[UIApplication sharedApplication]
scheduleLocalNotification:localnotifit];

 

}

 

// 注册公告(iOS 八之后的挂号文告方法,iOS 八在此从前暂不做牵线)

UIUserNotificationSettings *settings = [UIUserNotificationSettings
settingsForTypes:UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge | UIUserNotificationTypeSound
categories:nil];www.Bkjia.com

[[UIApplication sharedApplication]
registerUserNotificationSettings:settings];

 

return YES;

}

 

// 接收到布告后触发的格局,唯有在App进入前台的时候才会进行。

-(void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification

{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@提示message:@恭喜你受骗了 delegate:nil cancelButtonTitle:nil
otherButtonTitles:@笔者相当的慢意, nil];

[UIApplication sharedApplication]澳门葡京备用网址 ,.applicationIconBadgeNumber = 0;

[alert show];

 

// 撤废公告

// [[UIApplication sharedApplication] cancelAllLocalNotifications];

}

注册本地通告(推送)
注:按Home键让App进入后台运营时,方可查看文告。 –
(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOption…

1.第叁在didFinishLaunchingWithOptions方法内加多代码,IOS八推送消息首先要获得用户的允许,在初次安装App时会提示用户是或不是同意程序推送新闻,此办法是App第2次运营的时候被施行2次,每趟从后台激活时不施行该方法.

func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)
-> Bool {

if (UIDevice.currentDevice().systemVersion as NSString).floatValue >=
8 {

//APService.registerForRemoteNotificationTypes(

//UIUserNotificationType.Badge.rawValue |

//UIUserNotificationType.Sound.rawValue |

//UIUserNotificationType.Alert.rawValue,

//categories: nil)

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes:

UIUserNotificationType.Badge |

UIUserNotificationType.Sound |

UIUserNotificationType.Alert, categories: nil))

}

APService.setupWithOption(launchOptions)

return true

}

2.有多少个法子要说一下,

壹.func application威尔ResignActive(application: UIApplication){}
当App既将跻身后台、锁屏、有电话进来时会触发此事件

2.func applicationDidEnterBackground(application: UIApplication) {}
当App进入后台时触发此事件

三.func application威尔EnterForeground(application: UIApplication) {}
当App从后台将要再次回到前台时触发此事件

四.func applicationDidBecomeActive(application: UIApplication)
{}当App产生活动状态时触发此事件

五.func application威尔Terminate(application: UIApplication) {}
当App退出时触发此方法,一般用来保存有些特定的多寡

那会儿在applicationDidEnterBackground方法内写入以下代码:

func applicationDidEnterBackground(application: UIApplication) {

// 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.

UIApplication.sharedApplication().cancelAllLocalNotifications()

var notification = UILocalNotification()

//notification.fireDate = NSDate().dateByAddingTimeInterval

//setting timeZone as localTimeZone

notification.timeZone = NSTimeZone.localTimeZone()

notification.repeatInterval = NSCalendarUnit.CalendarUnitDay

notification.alertTitle = “This is a local notification”

notification.alertBody = “Hey,It‘s great to see you again”

notification.alertAction = “OK”

notification.soundName = UILocalNotificationDefaultSoundName

//setting app‘s icon badge

notification.applicationIconBadgeNumber = 1

var userInfo:[NSObject : AnyObject] = [NSObject : AnyObject]()

userInfo[“kLocalNotificationID”] = “LocalNotificationID”

userInfo[“key”] = “Attention Please”

notification.userInfo = userInfo

//UIApplication.sharedApplication().scheduleLocalNotification(notification)

//UIApplication.sharedApplication().presentLocalNotificationNow(notification)

application.presentLocalNotificationNow(notification)

}

那会儿将按Home键将App切换成后台时会有一条推送新闻,App角标变为了“一”

三.当用户点击音信时会触发didReceiveLocalNotification事件,在那个事件内写些代码:

func application(application: UIApplication, didReceiveLocalNotification
notification: UILocalNotification) {

UIApplication.sharedApplication().cancelAllLocalNotifications()

let userInfo = notification.userInfo!

let title = userInfo[“key”] as! String

var alert = UIAlertView()

alert.title = title

alert.message = notification.alertBody

alert.addButtonWithTitle(notification.alertAction!)

alert.cancelButtonIndex = 0

alert.show()

//APService.showLocalNotificationAtFront(notification, identifierKey:
nil)

}

4.当程序处于活动状态的时候解除ICON的角标

func applicationDidBecomeActive(application: UIApplication) {

// 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.

//setting the desk top application icon‘s badge as zero

//application.applicationIconBadgeNumber = 0

application.cancelAllLocalNotifications()

application.applicationIconBadgeNumber = 0

}

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*
*
Website