How iOS enables local push, compatible with iOS8

Source: Internet
Author: User
Tags set time uikit

If you want to be compatible with IOS8 for local push in iOS, it is important to note that iOS8 should be registered with the following statement when implementing a local push.

[[uiapplication sharedapplication] registerusernotificationsettings:mysettings];

As for the IOS8 version of the practice is not much to say, directly on the code. Create a new OC class file (Notificationhelper) and declare the relevant method in NotificationHelper.h as follows:

#import <UIKit/UIKit.h><UIApplicationDelegate>{}-(void) Addnotifiction: (nsstring*) Firedate Keya: (nsstring*) Key Messagea: (nsstring*) message
-(void) Removelocalnotificationbykey: (nsstring*) key;
-(void) removelocalallnotification;
-(void) Registerlocalnotification: (uiapplication*) application;
+ (notificationhelper*) shareinstance;
@ End

The implementation method in the NOTIFICATIONHELPER.M file is as follows:

#import"NotificationHelper.h"@implementation notificationhelperstatic Notificationhelper*instance;//implementation of a single case+ (notificationhelper*) shareinstance{static dispatch_once_t Oncetoken; Dispatch_once (&oncetoken,^{instance=[[Super Allocwithzone:null] init];    }); return instance;}    Push processing [registration message notification]-(void) Registerlocalnotification: (uiapplication*) application{ Application.applicationiconbadgenumber = 0;//Clears the number on the app icon
Key: Plus version of control # if __iphone_os_version_max_allowed >= __iphone_8_0//The following line must only run under IOS 8. This runtime check prevents//It is from running if it doesn ' t exist (such as running under IOS 7 or earlier). Uiusernotificationtype types = Uiusernotificationtypebadge | Uiusernotificationtypesound | Uiusernotificationtypealert; Uiusernotificationsettings *mysettings = [uiusernotificationsettings settingsfortypes:types categories:nil]; if ([Application respondstoselector: @selector (registerusernotificationsettings:)]) {[[UIApplication sharedappli cation] registerusernotificationsettings:mysettings]; } #endif}-(void) Addnotifiction: (nsstring*) firedate Keya: (nsstring*) Key Messagea: (nsstring*) message{NSLog (@ " Addnotifiction "); Uilocalnotification *localnotification = [[Uilocalnotification alloc] init]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [Formatter setdateformat:@ "HH:mm:ss"]; NSDate *now = [Formatter Datefromstring:firedate];//the time the notification was triggered
If Firedate is passed in the XX:XX:XX format at a fixed point in time to send a notification, if the pass-through is the XX format that is sent from now on XX seconds after sending the notification if (today = = nil) {nstimeinterval secs = [fi Redate Doublevalue]; now = [NSDate datewithtimeintervalsincenow:secs]; } localnotification.firedate = Now; Set time zone Localnotification.timezone = [Nstimezone defaulttimezone]; After triggering, the pop-up warning box displays the content localnotification.alertbody = message; Localnotification.alertaction = nslocalizedstring (@ "View Details", nil); The sound when triggered (the system default sound chosen here) Localnotification.soundname = Uilocalnotificationdefaultsoundname; Trigger Frequency (Repeatinterval is an enumeration value that can be selected per minute, hourly, daily, yearly, etc.) Localnotification.repeatinterval =Kcfcalendarunitday;//test with temporary write dead for every other day 0: do not repeat
//The number of unread notifications that need to be displayed on the app icon (set to 1 o'clock, multiple notifications are unread, the system will automatically add 1 if no readings are required, here you can set 0)
Localnotification.applicationiconbadgenumber = 1;
//Set the ID of the notification, which can be used to notify the removal or to pass other values, which can be obtained when the notification is triggered
localnotification.userinfo = @{@ "id": key};
//register for local notifications
[[UIApplication sharedapplication] schedulelocalnotification:localnotification];
[localnotification release];
}
/** Removelocalnotificationbykey */-(void) Removelocalnotificationbykey: (nsstring*) key {//Remove all local notifications Nsarray * notifications = [UIApplication sharedapplication].scheduledlocalnotifications; Set the notification ID to remove nsstring *notificationid = key; The traversal is removed for the (uilocalnotification *localnotification in notifications) {//the ID of each notification is taken out for comparison if ([[Localnotification.user Info objectforkey:@ "id"] Isequaltostring:notificationid]) {//remove one notification [[UIApplication sharedapplication] Cancellocalnotification:localnotification]; }}}-(void) removelocalallnotification {[[uiapplication sharedapplication] cancelalllocalnotifications];} @end

Examples of usage:

For example, when the application starts, it is called in the Didfinishlaunchingwithoptions method:

[[Notificationhelper shareinstance] registerlocalnotification:application];

Register and version control, and call when you need to send a notification:

[[Notificationhelper shareinstance] addnotifiction: "18:30:30" Keya: "Key" Messagea: "Can collect energy!" " ]

Complete. Since the company's hand-tour projects require local push, our project is to use the Quick cocos2d-x engine, which uses LUA to write scripts and interfaces. This poses a problem: how to write a friendly interface to enable Lua to invoke OC for push, so that all the logic is implemented in Lua.

Next time you are free to talk.

How iOS enables local push, compatible with iOS8

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.