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


主要方法:

//自定義推送
- (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(@"推送前台 推送前台");
    }
}

但是要注意的是,自定義通知時,只有程序在后台運行才有提示音,程序在前台運行時只有提示,沒有聲音,所以需要在前台自己設置播放聲音.不知道還有沒有其他解決辦法

//判斷前台
-(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:

  "_OBJC_CLASS_$_JPUSHRegisterEntity", referenced from:

      objc-class-ref in AppDelegate.o

  "_OBJC_CLASS_$_JPUSHService", referenced from:

      objc-class-ref in LoginViewController.o

      objc-class-ref in AppDelegate.o

ld: symbol(s) not found for architecture i386

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

 

原因查了很多地方,后來才發現只是因為打包時把product-schemes-run的環境改成release了,改回debug就沒這個錯誤了.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM