Message push-local push

Source: Internet
Author: User

There are two types of message pushes:

1. Native push (local Notification)

2. Remote push (remotes Notification)

The role of message push

Let the app not be in the foreground and tell the user what's going on inside the app

Effect of message push

1. No effect

2. Banners display specific content at the top of the screen

3. Remind Uialertcontroller to display specific content in the middle of the screen

4. Display a banner on the lock screen in the settings of the phone to make different effects settings

5. You can change the number of reminders shown above the app icon

Note: When sending a push notification, if the app is running in the foreground, the push notification will not be presented after the notification is sent, whether the app is open or closed, the push will be sent as scheduled, but the user may not be able to receive it as expected

However, registration is required after iOS8.

First in the APPDELEGATE.M file, you need to determine the version information based on whether the version information is greater than 8.0 settings push style, the specific code is as follows:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {

/*

Style of push notifications

Uiusernotificationtypenone no style

Uiusernotificationtypebadge change the upper right corner of the application number

Uiusernotificationtypesound with sound

Uiusernotificationtypealert Popup prompt

*/

Determine if version information is greater than 8.0 set the style of the push

if ([Uidevice currentdevice].systemversion.doublevalue >= 8.0) {

Uiusernotificationsettings *settings = [Uiusernotificationsettings settingsfortypes:uiusernotificationtypealert| uiusernotificationtypebadge| Uiusernotificationtypesound Categories:nil];

Registering a Notification object

[Application registerusernotificationsettings:settings];

}

Use as a jump when clicking push

if (launchoptions [Uiapplicationlaunchoptionslocationkey]) {

UILabel *label = [[UILabel alloc]initwithframe:cgrectmake (100, 400, 200, 100)];

Label.text = [NSString stringwithformat:@ "%@", launchoptions];

Label.backgroundcolor = [Uicolor Lightgraycolor];

Label.font = [Uifont systemfontofsize:14];

Label.numberoflines = 1;

[Self.window.rootViewController.view Addsubview:label];

}

return YES;

}

When you click the notification to open the app, the method will be called when the foreground receives a notification.

-(void) Application: (UIApplication *) application didreceivelocalnotification: (uilocalnotification *) notification{

UILabel *label = [[UILabel alloc]initwithframe:cgrectmake (100, 400, 200, 100)];

Label.backgroundcolor = [Uicolor Lightgraycolor];

Label.font = [Uifont systemfontofsize:14];

Label.numberoflines = 1;

[Self.window.rootViewController.view Addsubview:label];

}

Local message push is implemented by triggering local message sending object via button in VIEWCONTROLLER.M file

-(void) Viewdidload {

[Super Viewdidload];

Trigger local message Sending object via button

UIButton *button = [UIButton buttonwithtype:uibuttontypecustom];

Button.frame = CGRectMake (100, 100, 100, 50);

[Button settitle:@ "add notification" forstate:uicontrolstatenormal];

Button.backgroundcolor = [Uicolor Browncolor];

Button.layer.cornerRadius = 6;

[Button addtarget:self action: @selector (doit:) forcontrolevents:uicontroleventtouchupinside];

[Self.view Addsubview:button];

Remove Message push

UIButton *button1 = [UIButton buttonwithtype:uibuttontypecustom];

Button1.frame = CGRectMake (100, 200, 100, 50);

[Button1 settitle:@ "Remove notification" forstate:uicontrolstatenormal];

Button1.backgroundcolor = [Uicolor Browncolor];

Button1.layer.cornerRadius = 6;

[Button1 addtarget:self Action: @selector (remove) forcontrolevents:uicontroleventtouchupinside];

[Self.view Addsubview:button1];

}

-(void) doit: (UIButton *) sender{

1. Create a local notification object

Uilocalnotification *localnotification = [[Uilocalnotification alloc]init];

2. Setting specific properties

2.1 Set the time for notifications to be sent

Localnotification.firedate = [NSDate datewithtimeintervalsincenow:3];

2.2 Setting notifications to send content

Localnotification.alertbody = @ "The owner has come to the news";

2.3 Set whether a prompt box is displayed

Localnotification.hasaction = YES;

2.4 Setting up a prompt box

Localnotification.alertaction = @ "quick to see";

2.5 Setting the number of app tips

Localnotification.applicationiconbadgenumber = 123;

2.6 Setting the Sound of application hints

Localnotification.soundname = Uilocalnotificationdefaultsoundname;

3. To dispatch local push notifications

[[UIApplication sharedapplication]schedulelocalnotification:localnotification];

[[UIApplication sharedapplication]setapplicationiconbadgenumber:0];

}

How to remove a message push

-(void) remove{

[[UIApplication sharedapplication]cancelalllocalnotifications];

}

Message push-local 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.