IOS 極光推送自定義通知遇到的一些坑

主要方法:app

//自定義推送
- (void)networkDidReceiveMessage:(NSNotification *)notification {
    NSDictionary * userInfo = [notification userInfo];//
    NSString *contentUser = [userInfo valueForKey:@"content"];//content:獲取推送的內容
    NSDictionary *extras = [userInfo valueForKey:@"extras"];//extras:獲取用戶自定義參數
    NSString *customizeField1 = [extras valueForKey:@"customizeField1"]; //服務端傳遞的Extras附加字段,key是本身定義的     customizeField1:根據自定義key獲取自定義的value
    NSLog(@"接收到自定義通知 content = %@,extras = %@,customizeField1 = %@",contentUser,extras,customizeField1);
    //極光自定義通知
    JPushNotificationContent *content = [[JPushNotificationContent alloc] init];
    //content.title = @"Test Notifications";
    //content.subtitle = @"2016";
    content.body = contentUser;
    content.badge = @1;
    content.categoryIdentifier = @"Custom DAKQQSB DingDan";
    content.sound = @"test.caf";
    // 5s後提醒 iOS 10 以上支持
    JPushNotificationTrigger *trigger1 = [[JPushNotificationTrigger alloc] init];
    if(IOS10_OrLater)
    {
        trigger1.timeInterval = 0.001;
    }
    else
    {
        //5s後提醒,iOS10如下支持
        trigger1.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    }
    
    JPushNotificationRequest *request = [[JPushNotificationRequest alloc] init];
    request.requestIdentifier = content.categoryIdentifier;
    request.content = content;
    request.trigger = trigger1;
    request.completionHandler = ^(id result) {
        NSLog(@"極光推送結果返回:%@", result);
    };
    [JPUSHService addNotification:request];
    if([self runningInForeground])
    {
        NSLog(@"推送前臺 推送前臺");
    }
}

可是要注意的是,自定義通知時,只有程序在後臺運行纔有提示音,程序在前臺運行時只有提示,沒有聲音,因此須要在前臺本身設置播放聲音.不知道還有沒有其餘解決辦法url

//判斷前臺
-(BOOL) runningInForeground
{
    UIApplicationState state = [UIApplication sharedApplication].applicationState;
    BOOL result = (state == UIApplicationStateActive);
    
    return result;
}
//播放音樂
-(void)playSound{
    SystemSoundID soundID;
    //NSBundle來返回音頻文件路徑
    NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"caf"];
    //創建SystemSoundID對象,可是這裏要傳地址(加&符號)。 第一個參數須要一個CFURLRef類型的url參數,要新建一個NSString來作橋接轉換(bridge),而這個NSString的值,就是上面的音頻文件路徑
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundFile], &soundID);
    //播放提示音 帶震動
    AudioServicesPlayAlertSound(soundID);
    //播放系統聲音
    //    AudioServicesPlaySystemSound(soundID);
}
-----------錯誤記錄

Undefined symbols for architecture i386:spa

  "_OBJC_CLASS_$_JPUSHRegisterEntity", referenced from:debug

      objc-class-ref in AppDelegate.ocode

  "_OBJC_CLASS_$_JPUSHService", referenced from:對象

      objc-class-ref in LoginViewController.oblog

      objc-class-ref in AppDelegate.oit

ld: symbol(s) not found for architecture i386io

clang: error: linker command failed with exit code 1 (use -v to see invocation)class

 

緣由查了不少地方,後來才發現只是由於打包時把product-schemes-run的環境改爲release了,改回debug就沒這個錯誤了.

相關文章
相關標籤/搜索