【发布时间】:2017-05-20 01:26:35
【问题描述】:
我无法从我的 php 获得推送通知,但我可以从 online site for testing 获得通知。
我的 iOS 项目步骤如下:
为“推送通知”添加设置功能更改为开启,并且后台模式选择了“远程通知”和“后台获取”。
添加“UserNotifications.frameworks”
在 AppDelegate 中添加“”
4.在Appdelegate.m中添加如下代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
if( !error )
{
[[UIApplication sharedApplication] registerForRemoteNotifications]; // required to get the app to do anything at all about push notifications
NSLog( @"Push registration success." );
}
else
{
NSLog( @"Push registration FAILED" );
NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription );
NSLog( @"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion );
}
}];
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"push settings:%@",settings);
}];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken NS_AVAILABLE_IOS(3_0){
NSString *deviceTokenString = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@""]];
deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"device token:%@",deviceTokenString);
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
UNNotificationRequest *request = notification.request;
UNNotificationContent *content = request.content;
NSDictionary *userInfo = content.userInfo;
NSNumber *badge = content.badge;
NSString *body = content.body;
UNNotificationSound *sound = content.sound;
NSString *subtitle = content.subtitle;
NSString *title = content.title;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
NSLog(@"iOS10 get remote notify:%@",userInfo);
}else {
NSLog(@"iOS10 get local notify:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
}
completionHandler(UNNotificationPresentationOptionBadge|
UNNotificationPresentationOptionSound|
UNNotificationPresentationOptionAlert);
}
// notify click event
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
UNNotificationRequest *request = response.notification.request;
UNNotificationContent *content = request.content;
NSDictionary *userInfo = content.userInfo;
NSNumber *badge = content.badge;
NSString *body = content.body;
UNNotificationSound *sound = content.sound;
NSString *subtitle = content.subtitle;
NSString *title = content.title;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
NSLog(@"iOS10 remote notify:%@",userInfo);
}else {
NSLog(@"local notify:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
}
completionHandler();
}
- 我可以得到以下日志:
推送设置UNNotificationSettings 0x170089510: 授权状态,授权,通知中心设置,启用, soundSetting,启用,badgeSetting:启用,lockScreenSetting: 已启用,alertSetting:不支持,carPlaySetting:已启用, alertStyle , 提醒
推送注册成功。
设备令牌:我的设备令牌ID
- 我将 PHP 代码设置如下:
<?php $deviceToken = $row['my device token ID']; // Put your private key's passphrase $passphrase = 'my password'; $message = 'My first push notification!'; //////////////////////////////////////////////////////////////////////////////// $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); stream_context_set_option($ctx, 'ssl', 'cafile', 'entrust_2048_ca.cer'); // Open a connection to the APNS server $fp = stream_socket_client( 'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); // $fp = stream_socket_client( // 'ssl://gateway.push.apple.com:2195', $err, // $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); if (!$fp) exit("Failed to connect: $err $errstr" . PHP_EOL); echo 'Connected to APNS' . PHP_EOL; // Create the payload body $body['aps'] = array('alert' => $message,'sound' => 'default'); // Encode the payload as JSON $payload = json_encode($body); // Build the binary notification $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; // Send it to the server $result = fwrite($fp, $msg, strlen($msg)); if (!$result) echo 'Message not delivered' . PHP_EOL; else echo 'Message successfully delivered' . PHP_EOL; // Close the connection to the server fclose($fp); ?>
-
然后我在我的mac中运行php,我运行localhost/push.php,我可以从网络上获取消息:
连接到 APNS 消息已成功传递
但我的应用无法获取任何日志,也无法获取消息通知。
我使用的是开发证书和provision,所以我的测试推送站点设置为gateway.sandbox.push.apple.com:2195。
有没有人知道为什么我收不到通知,但我可以从在线考试站点收到通知?
非常感谢。
【问题讨论】:
标签: ios notifications push