1. 程式人生 > >本地推送,遠端推送(JPUSHService極光推送例)

本地推送,遠端推送(JPUSHService極光推送例)

本地推送,例:app鬧鐘,定時提醒==

    /*

    //iOS8以後需要先註冊本地通知,需要經過使用者的同意

    if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {

        UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];

        [[UIApplication sharedApplication] registerUserNotificationSettings:setting];

    }

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];

    //觸發的時間

    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];

    //通知的內容

    localNotification.alertBody = @"時間到了

,該起床了";

    //啟動通知

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

    //設定應用程式顯示的徽標

    [UIApplication sharedApplication].applicationIconBadgeNumber = 99;

*/

 遠端推送


例:

#import "AppDelegate.h"

#import "JPUSHService.h"

staticNSString *appKey = @"18a57d31ebab4f9be6932d61";

staticNSString *channel = @"Publish channel";

static BOOL isProduction = FALSE;

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//註冊遠端推送

if ([[UIDevicecurrentDevice].systemVersionfloatValue] >= 8.0) {

//可以新增自定義categories

        [JPUSHServiceregisterForRemoteNotificationTypes:(UIUserNotificationTypeBadge |

                                                          UIUserNotificationTypeSound |

                                                          UIUserNotificationTypeAlert)

                                              categories:nil];

    } else {

//categories 必須為nil

        [JPUSHServiceregisterForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |

                                                          UIRemoteNotificationTypeSound |

                                                          UIRemoteNotificationTypeAlert)

                                              categories:nil];

    }

    [JPUSHService setupWithOption:launchOptions appKey:appKey

                          channel:channel apsForProduction:isProduction];

return YES;

}

//將令牌傳送給極光推送伺服器

- (void)application:(UIApplication *)application

didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

       [JPUSHService registerDeviceToken:deviceToken];

}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{

    [JPUSHServicehandleRemoteNotification:userInfo];

    NSLog(@"收到通知");

}

//已經接收到遠端推送

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    [JPUSHServicehandleRemoteNotification:userInfo];

    completionHandler(UIBackgroundFetchResultNewData);

}