主要方法:
//自定义推送
- (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 = @;
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:];
} 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就没这个错误了.