简体   繁体   中英

Can't receive the push notification message from my php, but the online test site push is can get the message

I can't get push notification from my php, But I can get the notification from the online site for testing .

My iOS project step is below:

  1. add the setting Capabilities change to on for "Push notifications", And Background Mode selected the "remote notifications" and "background fetch".

  2. add the " UserNotifications.frameworks"

  3. In the AppDelegate add the ""

4.In the Appdelegate.m add below code:

 - (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();
 }
  1. I can get the below log:

push settings UNNotificationSettings 0x170089510: authorizationStatus, Authorized, notificationCenterSetting, Enabled, soundSetting, Enabled, badgeSetting:Enabled, lockScreenSetting: Enabled, alertSetting: NotSupported, carPlaySetting: Enabled, alertStyle , Alert

Push registration success.

device token:my device token ID

  1. I set the PHP code like below:
 <?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); ?> 
  1. then I run the php in my mac, I run the localhost/push.php , I can get the message from the web:

    Connected to APNS Message successfully delivered

  2. But I can't get the any log in my app, and can't get the message notification.

I am use the development certificate and provision, so my test push site is set the gateway.sandbox.push.apple.com:2195.

Have anyone known why I can't get the notification, but I can get the notification from the online test site?

Thank you very much.

I remember to have encountered same issue once, and I am not quite sure how I resolved it, as I chose to go with OneSignal later on as I didn't have a backend developer to write code for me. But could you once try replacing ssl in your line numbers 5,6 and 7 of the code with tls and try?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM