Uilocalnotification enables local notification implementation reminders in IOS development
Source: Internet
Author: User
<span id="Label3"></p>Apple mobile phone development information hint push way, a kind of remote server push (APNS) and uilocalnotification local notification, Let me introduce the second way to Use. These two days in the alarm clock to make a function, the use of the local notification function, record the relevant knowledge as Follows:<p><p>1. Definition and use of local notifications:</p></p><p><p>Local notifications are instances of uilocalnotification, and there are three main types of properties:</p></p><p><p>Scheduled time, period, To specify the time and date when the iOS system sends notifications;</p></p><p><p>Notification type, which includes the warning message, the caption of the action button, the badge (digital marker) on the app icon, and the sound being played;</p></p><p><p>Custom data, Local notifications can contain a dictionary type of local Data.</p></p><p><p>Limits on the number of local notifications, iOS allows up to 64 recent local notifications, and local notifications exceeding the limit will be ignored by ios.</p></p><pre>Uilocalnotification *notification=<span style="color: #000000;"><span style="color: #000000;">[[uilocalnotification alloc] init]; Notification.firedate</span></span>=[nsdate Datewithtimeintervalsincenow:<span style="color: #800080;"><span style="color: #800080;">Ten</span></span><span style="color: #000000;"><span style="color: #000000;">]; Notification.alertbody</span></span>=<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">the alarm clock rang. </span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">; Notification.alerttitle</span></span>=<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Please turn</span> on the alarm</span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">; Notification.repeatinterval</span></span>=<span style="color: #000000;"><span style="color: #000000;">nscalendarunitsecond; Notification.applicationiconbadgenumber</span></span>=<span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">; Notification.userinfo</span></span>[email protected] {<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">name</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Zhangsanfeng</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">}; Notification.soundname</span></span>=<span style="color: #000000;"><span style="color: #000000;">uilocalnotificationdefaultsoundname; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">[[uiapplication sharedapplication]schedulelocalnotification:notification]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">[[uiapplication sharedapplication]presentlocalnotificationnow:notification];</span></span></pre><p><p>2. Cancel Local notification:</p></p><pre>UIApplication *app=<span style="color: #000000;"><span style="color: #000000;">[uiapplication sharedapplication]; Nsarray</span></span>*array=<span style="color: #000000;"><span style="color: #000000;">[app scheduledlocalnotifications]; NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">%ld</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, array.count); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(uilocalnotification * Local<span style="color: #0000ff;"><span style="color: #0000ff;">inch</span></span><span style="color: #000000;"><span style="color: #000000;">Array) {nsdictionary</span></span>*dic=<span style="color: #000000;"><span style="color: #000000;">local.userinfo; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>([dic[<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">name</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>] Isequal:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Zhangsanfeng</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">delete the specified notification</span></span><span style="color: #000000;"><span style="color: #000000;">[app cancellocalnotification:local]; } } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">You can also use [app cancelalllocalnotifications] to delete all notifications; </span></span></pre><p><p>3, the response of local notification:</p></p><p><p>If a local notification has been registered, when the client responds to the Notification:</p></p><p><p>a, the application in the background, the local notification will be sent to the <strong>device</strong> with a remote notification of the same reminder, the style of the reminder is set by the user in the phone settings</p></p><p><p>b, the application is running, the <span style="text-decoration: underline;"><strong>device will not (but the Application)</strong></span> receive a reminder, but will walk the method in the application delegate:</p></p><pre><pre>-(<span style="color: #0000ff;">void</span>) application: (uiapplication *) application didreceivelocalnotification: (uilocalnotification *<span style="color: #000000;">) notification{ </span><span style="color: #008000;">//</span> <span style="color: #008000;">If you want to implement a program in the background of the kind of reminders, you can add the relevant code in this method (add a warning View)</span>}</pre></pre><p><p>It is important to note that in case a, if the user clicks on the reminder to enter the application, the callback method that receives the local notification will also be executed, in which case if you add the above code, there will be two consecutive prompts, in order to solve the problem, the code is as Follows:</p></p><pre>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) application: (uiapplication *) application didreceivelocalnotification: (uilocalnotification *<span style="color: #000000;"><span style="color: #000000;">) notification{</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">determine the current running state of the application, and if it is active, make a reminder, or do not alert</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(application.applicationstate = =<span style="color: #000000;"><span style="color: #000000;">Uiapplicationstateactive) {uialertcontroller</span></span>*alert=[uialertcontroller alertcontrollerwithtitle:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">End of timing</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">message:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">your alarm has arrived.</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">preferredstyle:uialertcontrollerstylealert]; Uialertaction</span></span>*action1=[uialertaction actionwithtitle:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">yes, I know.</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">Style:uialertactionstyledefault Handler:</span></span>^ (uialertaction*<span style="color: #000000;"><span style="color: #000000;">Action) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">[self dosomething];</span></span><span style="color: #000000;"><span style="color: #000000;"> }]; Uialertaction</span></span>*action2=[uialertaction actionwithtitle:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Stop arguing, My day.</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">style:uialertactionstyledestructive</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Warning style, Red</span></span>handler:^ (uialertaction*<span style="color: #000000;"><span style="color: #000000;">Action) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">[self dosomething];</span></span><span style="color: #000000;"><span style="color: #000000;"> }]; [alert addaction:action2]; [alert addaction:action1]; [self Presentviewcontroller:alert Animated:yes completion:nil]; }}</span></span></pre><p><p>4, need to note: what is the difference between and <code>presentLocalNotificationNow</code><code>scheduleLocalNotification?</code></p></p><p><p>There's no difference right here, but using the <code>scheduleLocalNotification</code> can schedule it at whatever time to want, not only in one second. B UT would <code>presentLocalNotificationNow</code> show one in exactly one second, not in 0.5 or 2.0 on IOS 8, for Example.</p></p><p><p>If your application is not active, local notifications will be effective;</p></p><p><p>Uilocalnotification enables local notification implementation reminders in IOS development</p></p></span>
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