Local Notifications

http://iphonedevelopertips.com/core-services/local-notifications.html

 

In this post I’ll show you the basics for working with local notifications, which are notifications generated from the device itself, versus a remote notification (aka push notification) that is sent from a remote server. Local notifications are useful when you need to schedule a reminder or otherwise notify the user of a future event.

Creating and Processing Local Notifications

Local notifications are scheduled for a future date by creating a UILocalNotification object. When creating the notification you can also specify the type of notification(s) to send, a badge, alert or sound.

If a local notification arrives on the application that is not running in the foreground, the notification is delivered (that is, the alert shown, the badge set, sound played). If the notification is an alert, a dialog is shown to the user, along with an option to open the application. If the user chooses to open the app, the method didFinishLaunchingWithOptions in the application delegate is called passing in the local notification object.

If the application is running when a local notification is received, the methoddidReceiveLocalNotification is called, again, passing in the local notification object.

Screenshots

The app I’ve written to work with notification has a range of buttons to schedule various types of notifications:

Once you choose an option, the notification is scheduled – to keep things simple, in my application I’ve set all notifications to arrive in 60 seconds. The screenshot below shows an incoming notification when the app has been put into the background.

The screenshot below is an alert I display from within the method called when a notification arrives.

Methods in the App Delegate

The application delegate contains the two methods needed for managing incoming notifications. The method didFinishLaunchingWithOptions is shown below. Notice the check of the launchOptions parameter early in the method, if this value is nil, this is a standard application startup. If the value is not nil, the application was started based on a notification presented to the user – in this case I show an alert along with a few debug messages, including the payload data received from the notification (more on this below).

The code in the later half of the method is typical startup stuff, creating a window and view controller, etc

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // Handle launching from a notification
  UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
 
  // If not nil, the application was based on an incoming notifiation
  if (notification) 
  {
    NSLog(@"Notification initiated app startup");
 
    // Access the payload content
    NSLog(@"Notification payload: %@", [notification.userInfo objectForKey:@"payload"]);
 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Launched with Options" 
                              message:@"Notification initiated app startup" 
                              delegate:self cancelButtonTitle:@"Ok" 
                              otherButtonTitles:nil];
    [alert show];
    [alert release];
  }
 
  // Reset the badge
  [application setApplicationIconBadgeNumber:0];
 
  // Create and initialize the window
  window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 
  // Create test view controller
  vc = [[TestViewController alloc] init];
 
  [window addSubview:vc.view];
  [window makeKeyAndVisible];
 
  return YES;
}

The second method in the application delegate is didReceiveLocalNotification which will be called when a notification arrives while the applications is running. In this method I show an alert and also write the notification payload to the debug console. For the payload, I am creating an array of objects, packaging the array within a dictionary and passing the later along with the notification.

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Launched with Options" 
                            message:@"Incoming notification in running app" 
                            delegate:self cancelButtonTitle:@"Ok" 
                            otherButtonTitles:nil];
  [alert show];
  [alert release];
 
  NSLog(@"Incoming notification in running app");
 
  // Access the payload content
  NSLog(@"Notification payload: %@", [notification.userInfo objectForKey:@"payload"]);
}
Scheduling a Local Notification

The buttons shown in the screenshot above are how we initiate creating and scheduling a local notification. Before handling each event there is some common code for creating aUILocalNotification object, setting the fire date and populating the data (payload) that will accompany the notification.

- (void)buttonPressed:(UIButton *)button
{
  UILocalNotification *localNotification = [[[UILocalNotification alloc] init] autorelease];
 
  if (!localNotification) 
    return;
 
  // Current date
  NSDate *date = [NSDate date]; 
 
  // Add one minute to the current time
  NSDate *dateToFire = [date dateByAddingTimeInterval:60];
 
  // Set the fire date/time
  [localNotification setFireDate:dateToFire];
  [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
 
  // Create a payload to go along with the notification
  NSArray *array = [NSArray arrayWithObjects:@"Value 1", @"Value 2", nil];
  NSDictionary *data = [NSDictionary dictionaryWithObject:array forKey:@"payload"];
  [localNotification setUserInfo:data];
 
  if (button == buttonAlert || button == buttonAll)
  {	
    // Setup alert notification
    [localNotification setAlertBody:@"Incoming Local Notification" ];
    [localNotification setAlertAction:@"Open App"];
    [localNotification setHasAction:YES];      
  }
 
  if (button == buttonBadge || button == buttonAll)
  {
    // Set badge notification, increment current badge value
    [localNotification setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber] + 1];
  }
 
  if (button == buttonSound || button == buttonAll)
  {
    // Setup sound notification
    [localNotification setSoundName:UILocalNotificationDefaultSoundName];			  
  }
 
  // Schedule the notification        
  [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Local Notification Xcode Project

Download Xcode Local Notification Example

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: flutter_local_notifications是一个Flutter插件,用于在移动设备上显示本地通知。它可以帮助开发者在应用程序中实现各种通知功能,例如提醒用户进行某项操作、通知用户有新消息或提醒用户完成某项任务等。该插件支持Android和iOS平台,并提供了丰富的API和配置选项,使开发者可以轻松地创建和管理本地通知。 ### 回答2: flutter_local_notifications 是一个 Flutter 插件,用于在本地进行通知的创建、显示和调度。此插件可让开发人员在应用程序内部创建和安排本地通知,通常用于为应用程序用户提供有关后台事件的提示。 此插件允许开发人员创建三种类型的通知:基本通知、计划通知和重复通知。基本通知是简单的通知消息,可包括标题、正文和指向应用程序的图标。计划通知是在特定的日期和时间向用户发出的通知,例如生日提醒、节日提醒、会议提醒等。重复通知是在特定间隔时间后重复出现的通知,例如每天定时提醒用户进行运动或吃药。 此插件使用 Android 和 iOS 平台的本地通知功能来实现本地通知。它提供了许多选项,例如可自定义通知的图标、声音和振动模式;可自定义通知的优先级和颜色,并具有特定到最小粒度(秒)的计划通知;还可以让开发人员在应用程序未运行时携带数据传递通知。 最后,flutter_local_notifications 插件是一个非常有用的 Flutter 插件,允许开发人员创建和安排本地通知,看起来与设备上的其余通知一样。此插件使用简单、功能强大,可帮助开发人员为应用程序用户提供更好的用户体验。 ### 回答3: Flutter是一种非常流行的移动应用程序开发框架。Flutter_local_notifications是Flutter中的一个插件,它提供了本地通知的功能,可以在用户离开应用的情况下向用户发送通知。 Flutter_local_notifications可以在Android和iOS上使用,并且它提供了更在深度的配置和灵活性,以满足用户的需求。它可以在应用程序的后台执行,并且它可以在用户设备的通知栏,锁屏上提醒用户。 Flutter_local_notifications使用简单易懂,只需要几行代码就可以实现。它可以用于多种类型的应用程序,如电商应用中的订单提醒,健康应用程序中的饮食计划,社交应用程序中的新消息提醒等等。 除了提供基本的通知配置,Flutter_local_notifications还可以自定义通知的图标,声音和振动等,从而让每个通知都更加个性化。此外,Flutter_local_notifications还提供了许多其他的功能,如定时通知,本地存储通知,红点提醒等等。 总之,Flutter_local_notifications是一种非常有用的Flutter插件,它可以为许多不同类型的应用程序提供强大的功能和定制化能力。如果你正在开发一个需要本地通知功能的应用程序,那么Flutter_local_notifications是一个非常不错的选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值