你好,歡迎來到IOS教程網

 Ios教程網 >> IOS使用技巧 >> IOS技巧綜合 >> ios如何實現本地推送,兼容ios8

ios如何實現本地推送,兼容ios8

編輯:IOS技巧綜合
[摘要]本文是對ios如何實現本地推送,兼容ios8的講解,對學習IOS蘋果軟件開發有所幫助,與大家分享。

如果要兼容IOS8在IOS中實現本地推送,關鍵是要注意:ios8在實現本地推送時需要通過如下語句進行注冊。

[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

至於IOS8之前版本的做法就不多說了,直接上代碼。新建oc類文件(NotificationHelper),在NotificationHelper.h中聲明相關方法如下:

復制代碼
#import <UIKit/UIKit.h>

@interface NotificationHelper:NSObject <UIApplicationDelegate>
{
}
-(void) addNotifiction:(NSString*) firedate keyA:(NSString*)key messageA:(NSString*)message
-(void)removeLocalNotificationByKey:(NSString*)key;
-(void)removeLocalAllNotification;
-(void) registerLocalNotification:(UIApplication*)application;
+(NotificationHelper*) shareInstance;
@end
復制代碼

在NotificationHelper.m文件中實現方法如下:

復制代碼
#import "NotificationHelper.h"

@implementation NotificationHelper
static NotificationHelper* instance;
//實現單例
+(NotificationHelper*) shareInstance
{
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        instance = [[super allocWithZone:NULL] init] ;
    });
    return instance ;
}
//推送處理[注冊消息通知]
-(void) registerLocalNotification:(UIApplication*)application
{
    application.applicationIconBadgeNumber = 0;//清除應用圖標上的數字
//關鍵:加上版本的控制 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0 // The following line must only run under iOS 8. This runtime check prevents // it from running if it doesn't exist (such as running under iOS 7 or earlier). UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings]; } #endif } -(void) addNotifiction:(NSString*) firedate keyA:(NSString*)key messageA:(NSString*)message { NSLog(@"addNotifiction"); UILocalNotification *localNotification = [[UILocalNotification alloc] init]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"HH:mm:ss"]; NSDate *now = [formatter dateFromString:firedate];//觸發通知的時間
  //如果firedate傳入的是XX:XX:XX格式在表示在固定的時間點發送通知,如果傳入的是XX格式表示從現在開始XX秒後發送通知 if(now == nil) { NSTimeInterval secs = [firedate doubleValue]; now = [NSDate dateWithTimeIntervalSinceNow:secs]; } localNotification.fireDate = now; //設置 時區 localNotification.timeZone = [NSTimeZone defaultTimeZone]; // 觸發後,彈出警告框中顯示的內容 localNotification.alertBody = message; localNotification.alertAction = NSLocalizedString(@"View Details", nil); // 觸發時的聲音(這裡選擇的系統默認聲音) localNotification.soundName = UILocalNotificationDefaultSoundName; // 觸發頻率(repeatInterval是一個枚舉值,可以選擇每分、每小時、每天、每年等) localNotification.repeatInterval = kCFCalendarUnitDay;//測試用暫時寫死為每隔一天 0:不重復
// 需要在App icon上顯示的未讀通知數(設置為1時,多個通知未讀,系統會自動加1,如果不需要顯示未讀數,這裡可以設置0)
localNotification.applicationIconBadgeNumber = 1;
// 設置通知的id,可用於通知移除,也可以傳遞其他值,當通知觸發時可以獲取
localNotification.userInfo = @{@"id" : key};
// 注冊本地通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
}
/**
 removeLocalNotificationByKey
 */
- (void)removeLocalNotificationByKey:(NSString*)key {
    // 取出全部本地通知
    NSArray *notifications = [UIApplication sharedApplication].scheduledLocalNotifications;
    // 設置要移除的通知id
    NSString *notificationId = key;
    // 遍歷進行移除
    for (UILocalNotification *localNotification in notifications) {
        // 將每個通知的id取出來進行對比
        if ([[localNotification.userInfo objectForKey:@"id"] isEqualToString:notificationId]) {
            // 移除某一個通知
            [[UIApplication sharedApplication] cancelLocalNotification:localNotification];
        }
    }
}

- (void)removeLocalAllNotification {
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}
@end
復制代碼

用法舉例:

比如在應用啟動的時候調在didFinishLaunchingWithOptions方法中調用:

[[NotificationHelper shareInstance] registerLocalNotification:application];

進行注冊和版本控制,在需要發送通知的時候調用:

[[NotificationHelper shareInstance]addNotifiction:"18:30:30" keyA:"key" messageA:"可以領取體力了!" ]

完畢。由於公司的手游項目需要使用到本地推送,而我們的項目是用quick cocos2d-x引擎,前端使用LUA編寫腳本和界面。這樣就面臨一個問題:如何編寫友好的接口讓lua能夠調用oc來實現推送,這樣的話所有的邏輯都在lua中實現。

下次有空再說。

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved