Local push of iOS (push)

Source: Internet
Author: User

There are two kinds of message notifications on iOS, one for local Notification and one for remote messages (Push Notification, also known as Remote Notification), designed both to alert users Now something new has happened that will entice the user to reopen the app. Local push can also be controlled by the server, for example, if there is a new message, push the message, but the premise is that the program must be open, and remote push, is through the Apple APNs server, pushed to the phone, the mobile phone in the push to specific which program, generally remote push to use more, first introduce the next local push, The following section describes remote push.

Local push:

First, register in appdelegate first:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {    [Application registerusernotificationsettings:[uiusernotificationsettings Settingsfortypes: Uiusernotificationtypealert| uiusernotificationtypebadge| Uiusernotificationtypesound categories:nil]];//Register Local push        //Override point for customization after application launch .    return YES;}

Then, push is implemented in the specific Viewcontroller:

-(Ibaction) Localpushnow: (ID) Sender {dispatch_async (dispatch_get_global_queue (dispatch_queue_priority_default, 0)        , ^{//local push uilocalnotification*notification = [[Uilocalnotification alloc]init];        NSDate * pushdate = [NSDate datewithtimeintervalsincenow:10];            if (notification! = nil) {notification.firedate = pushdate;            Notification.timezone = [Nstimezone defaulttimezone];            Notification.repeatinterval = Kcfcalendarunitday;            Notification.soundname = Uilocalnotificationdefaultsoundname;            Notification.alertbody = @ "Hello,world";            Notification.applicationiconbadgenumber = 0;            Nsdictionary*info = [nsdictionary dictionarywithobject:@ "test" forkey:@ "name"];            Notification.userinfo = info;                    [[UIApplication sharedapplication] schedulelocalnotification:notification]; }    });}

The push message is received in Appdelegate:

// receive local push

Receive local push-(void) Application: (UIApplication *) application didreceivelocalnotification: (Uilocalnotification *) notification{    NSLog (@ "%@", notification.alertbody);    Uilabel*label = [[UILabel alloc]init];    Label.frame = CGRectMake (0, 0,.);    Label.layer.cornerRadius = ten;    Label.backgroundcolor = [Uicolor blackcolor];    Label.text = Notification.alertbody;    Label.textcolor = [Uicolor whitecolor];    Label.font = [Uifont systemfontofsize:12];    Label.textalignment = Nstextalignmentcenter;        [Self.window Addsubview:label];}

The following conditions may occur in the process:

attempting to schedule a local notification ... With a sound but haven ' t received permission from the user to play sounds

attempting to schedule a local notification ... With a alert but haven ' t received permission from the user to display alerts

It may be because you are not registered, or the settings do not have the push feature turned on,

Local push of iOS (push)

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.