Use of iOS local alerts (nslocalnotification)

Source: Internet
Author: User

Effect Chart:




Figure I:


Core code:

JLVIEWCONTROLLER.M//local notification///Created by xinyou on 15-5-15. Copyright (c) 2015 vxinyou.
All rights reserved.


#import "JLViewController.h" @interface Jlviewcontroller ()-(ibaction) addlocalnotification;
    
@end @implementation Jlviewcontroller-(void) viewdidload {[Super viewdidload];
    }/* Three cases: 1. Applications are not closed, but run in the background. Click on the notification to enter the application, will call the Jlappdelegate Didreceivelocalnotification method, and if you have set notification Alertlaunchimage properties, you will first display the corresponding picture of the property,
 
 And then into the application.
    2. The application is turned off and does not run in the background.
    Clicking on the application icon to start the application will call the Jlappdelegate Didfinishlaunchingwithoptions Method 3. The application is turned off and does not run in the background.
 Click on the notification to start the application, will also call the Jlappdelegate Didfinishlaunchingwithoptions method so, how to distinguish is by clicking the application icon or by clicking the notice into the application. See Jlappdelegate didfinishlaunchingwithoptions Method * */** * ADD LOCAL notification * * * (ibaction) addlocalnotification {//1
    
    . Create notification Uilocalnotification *notification = [[Uilocalnotification alloc] init];
    2. Set the properties//The following code is the function of: "Sliding to start playing the game" when the screen is locked, as shown in figure. Notification.alertaction = @ "Start playing games ";
    
    The content of the notice notification.alertbody = @ "Slaughter Dragon Blade, click on send!!!"; Notifies the corresponding reminder number.
    
    When the notification comes, there is a reminder number Notification.applicationiconbadgenumber = 1 in the upper-right corner of the application icon. Recurrence interval for notifications (enumeration type).
    
    This is set to be notified once every one hours//notification.repeatinterval = Nscalendarunithour; The general setting is the startup picture (Launchimage) for the program.
It means that you need to display the picture when you re-enter the application by clicking on the notification.
    
    Notification.alertlaunchimage = @ "Default"; Notification time.
    
    Set to 4 seconds to display notification notification.firedate = [NSDate datewithtimeintervalsincenow:4.0];
    The UserInfo set here can be obtained in the Didfinishlaunchingwithoptions method in the JLAPPDELEGATE.M file.
    
    Notification.userinfo = @{@ "username": @ "Zhangsan"};
    
    3. Registration notice UIApplication *application = [UIApplication sharedapplication];
    
    Cancel the notification first, avoid repeating add [application cancellocalnotification:notification];
    
    Cancel all notices//[application cancelalllocalnotifications];
Sign up (add) notice [application schedulelocalnotification:notification]; } @end////JLAPPDELEGATE.M///Created by Xinyou on 15-5-15. Copyright (c) 2015 vxinyou.
All rights reserved. #import "JLAppDelegate.h" @implementation jlappdelegate-(BOOL) Application: (UIApplication *) Application DIDFINISHL Aunchingwithoptions: (nsdictionary *) launchoptions {//If launchoptions is empty, means to start the program by clicking the Apply icon (launchoptions = n IL) {Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "Prompt" message:@ "Normal startup Application" Delegate:nil CA
        
        ncelbuttontitle:@ "Determine" otherbuttontitles:nil, nil];
        
    [Alertview show]; }else{//if Launchoptions is not empty, it means to start the program by clicking the notification uilocalnotification *notification = Launchoptions[uiapplicationla
        
        Unchoptionslocalnotificationkey];
        
        Nsdictionary *userinfo = Notification.userinfo;
        
        NSString *username = userinfo[@ "username"];
        
        NSString *msg = [NSString stringwithformat:@ "click Notice to Start Application!username:%@", username]; Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "hint" messagE:msg delegate:nil cancelbuttontitle:@ "OK" otherbuttontitles:nil, nil];
    [Alertview show];
return YES; /** * User clicks on the notification to enter this application (application is not closed, but running in the background)/-(void) Application: (UIApplication *) Application Didreceivelocalnotification :(uilocalnotification *) notification{Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "Prompt" message:@ "click through
    
    The knowledge enters the application "Delegate:nil cancelbuttontitle:@" determines "otherbuttontitles:nil, nil";
[Alertview show]; }-(void) Applicationwillresignactive: (uiapplication *) application {//Sent when the application was about to Move from active to inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or the US
    Er quits the application and it begins the transition to the background state. Use the To pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates.
Games should to pause the game. }-(void)Applicationdidenterbackground: (uiapplication *) application {//use ' this ' to ' release shared resources, save user Data, invalidate timers, and store enough application state information to restore your application to its 
    In case it is terminated later. If your application supports background execution, this 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 this were paused (or not yet started) while the application is inactive.
If the application is 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.
Also applicationdidenterbackground:.
 } @end

SOURCE Download Address: https://github.com/liujie537192/LocalNotification

Related Article

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.