首页 > 代码库 > 小结Android和iOS中LocalNotification

小结Android和iOS中LocalNotification

用Unity开发游戏,总难免要用到Native Development,比如Notification功能。

本文只对LocalNotification进行小结,iOS上RemoteNotification在此未讨论。(后面发现Unity已经把iOS部分给封装好了)

Notification大致提供了两个功能:Register和Cancel,没找到Update的API,后来自己想了个Update的方法,就是先Cancel再Register.

 

iOS:

 1 //由于c#用long类型传递秒数,而long类型在C#中是64位,在OC中是32位,这样会导致后面参数读取出现问题,所以这边senconds用int64_t代替(也可用long long)
 2 + (void) registerLocalNotification:(int64_t)seconds withMessage:(NSString *)msg notificationId:(NSString*) notificationId
 3 {
 4     UILocalNotification *notification = [[UILocalNotification alloc] init];
 5     NSDate *now = [NSDate date];
 6     //通知触发的时间
 7     notification.fireDate = [now dateByAddingTimeInterval:(NSTimeInterval)seconds];
 8     //显示在通知中的信息
 9     notification.alertBody = msg;
10     //通知显示时的提示音 
11     notification.soundName = UILocalNotificationDefaultSoundName;
12     //桌面应用程序图标右上角显示的消息个数,这里每个消息都标记为1
13     notification.applicationIconBadgeNumber++;
14     
15     //设置通知标识符,以便后面取消该通知使用,如果不做取消,可不设置
16     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:notificationId, NOTIFICATION_ID, nil];
17     notification.userInfo = dict;
18     
19     //注册通知
20     [[UIApplication sharedApplication] scheduleLocalNotification:notification];
21 }
22 
23 //API没Update操作,故用先Cancel再Register来解决
24 + (void) updateLocalNotification:(int64_t)seconds withMessage:(NSString *)msg notificationId:(NSString *)notificationId
25 {
26     [Notifier cancelLocalNotification:notificationId];
27     [Notifier registerLocalNotification:seconds withMessage:msg notificationId:notificationId];
28 }
29 
30 + (void) cancelLocalNotification:(NSString *)notificationId
31 {
32     //获取当前应用程序已注册的所有本地通知
33     NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
34     for (UILocalNotification *notification in notifications)
35     {
36         //通过匹配原先注册时标记的Id号来查找
37         if ([[notification.userInfo objectForKey:NOTIFICATION_ID] isEqualToString:notificationId])
38         {
39             [[UIApplication sharedApplication] cancelLocalNotification:notification];
40             break;
41         }
42     }
43 }
View Code

 

Android:

 1 public class Notifier {
 2     
 3     /*
 4      * 由于Notification.when不起作用,故需要延时操作时采用postAtTime(Runnable r, Object token, long uptimeMillis)
 5      * 其中token用来标记该runnable,以便后面取消用
 6      * 注意:如果到了触发通知时,应用程序不在后台运行,则不会弹出通知,即app必须处于运行状态才有效
 7      */
 8     private static boolean initialized = false;
 9     private static Handler mHandler;
10     private static Handler getHandler() {
11         if (!initialized) {
12             initialized = true;
13             HandlerThread thread = new HandlerThread("Notifier");
14             thread.start();
15             mHandler = new Handler(thread.getLooper());
16         }
17         return mHandler;
18     }
19     
20     @SuppressWarnings("deprecation")
21     public static void registerLocalNotification(final Context context, final long seconds , final String message, final int notificationId) {
22         
23         final Notification notification = new Notification();
24         Resources res = context.getResources();
25         int app_icon = res.getIdentifier("app_icon", "drawable", context.getPackageName());
26         int app_name = res.getIdentifier("app_name", "string", context.getPackageName());
27         String title = context.getString(app_name);
28         notification.icon = app_icon; //图标一定要设置正确,不正确会导致通知栏无法显示
29         long[] vibrate = {0, 100, 200, 300}; // 0ms后 震动100ms,200ms后震动300ms,用振动时,需在AndroidManifest声明android.permission.VIBRATE权限
30         notification.vibrate = vibrate;
31         notification.defaults |= Notification.DEFAULT_SOUND;
32         notification.flags |= Notification.FLAG_AUTO_CANCEL;
33         notification.when = System.currentTimeMillis() + seconds * 1000;
34         notification.tickerText = message;
35         
36         Intent intent = new Intent(context, context.getClass());
37         PendingIntent contentIntent = PendingIntent.getActivity(context, app_name, intent, PendingIntent.FLAG_UPDATE_CURRENT);
38         notification.setLatestEventInfo(context, title, message, contentIntent);
39         
40         getHandler().postAtTime(new Runnable() {
41             @Override
42             public void run() {
43                 if (Utils.isAppOnBackground(context)) {
44                     getNotificationManager(context).notify(notificationId, notification);
45                 }
46             }
47         }, Integer.toString(notificationId), SystemClock.uptimeMillis() + seconds * 1000);
48     }
49     
50     private static NotificationManager getNotificationManager(Context context) {
51         return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
52     }
53     
54     public static void updateLocalNotification(Context context, long seconds, String message, int notificationId) {
55         getHandler().removeCallbacksAndMessages(Integer.toString(notificationId));
56         registerLocalNotification(context, seconds, message, notificationId);
57     }
58     
59     public static void cancelLocalNotification(int notificationId) {
60         getHandler().removeCallbacksAndMessages(Integer.toString(notificationId));        
61     }
62 }

 

Android版本由于在应用程序处于前台时也会显示通知,所以我在触发通知的地方做了应用程序是否处于后台的判断

 1     public static boolean isAppOnBackground(Context context) {
 2         ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
 3         List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
 4         for (RunningAppProcessInfo appProcess : appProcesses) {
 5             if (appProcess.processName.equals(context.getPackageName())) {
 6                 if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
 7                     return true;
 8                 } else {
 9                     return false;
10                 }
11             }
12         }
13         return false;
14     }

 

好了,关于LocalNotification的小结就到这了。如果读者有什么疑惑,可以回复下,大家一起探讨,互相学习。