Search code examples
phpiosamazon-web-servicesamazon-snsapn

How to register the IOS device token and receive message in the Amazon SNS (notification service)?


I am working on a IOS project and I would like to integrate the SNS to my project without simply using APN.

All I need is:

Ask the user whether to receive notification
get device ID
Store device ID
Send to each device ID using PHP

Since I have work for GCM android before , I have some basic idea about this, I downloaded the sample app from Amazon, and I achieve the first two step, that means I can get the device ID, but the problem are

it did not automatically store on Amazon, Also, when I manually insert the id to Amazon and tried to send the message, it does not success

Here is the code:

IOS side (AppDelegate.m)

#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    application.applicationIconBadgeNumber = 0;
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    if(launchOptions!=nil){
        NSString *msg = [NSString stringWithFormat:@"%@", launchOptions];
        NSLog(@"%@",msg);
        [self createAlert:msg];
    }
    return YES;
}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{
    NSLog(@"deviceToken: %@", deviceToken);
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error{
    NSLog(@"Failed to register with error : %@", error);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    application.applicationIconBadgeNumber = 0;
    NSString *msg = [NSString stringWithFormat:@"%@", userInfo];
    NSLog(@"%@",msg);
    [self createAlert:msg];
}

- (void)createAlert:(NSString *)msg {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received" message:[NSString stringWithFormat:@"%@", msg]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
}

@end

PHP warning message when send the notification

Success: arn:aws:sns:ap-southeast-1:447163929286:endpoint/APNS_SANDBOX/tuen_mun_travel_sandbox/46a38bf8-3d1a-35f9-817a-62cd56b3c502

Warning: Cannot modify header information - headers already sent by (output started at /home/tuenmunt/public_html/wp-content/plugins/push.php:67) in /home/tuenmunt/public_html/wp-admin/post.php on line 233

Warning: Cannot modify header information - headers already sent by (output started at /home/tuenmunt/public_html/wp-content/plugins/push.php:67) in /home/tuenmunt/public_html/wp-includes/pluggable.php on line 896

Thanks for helping


Solution

  • The warning looks like you are sending header information to the browser twice. Be sure to send the header information once to the browser for the same page. It would help if post.php and pluggable.php were posted here also so we could show you where this is happening. Regardless, you should be seeing the push notifications in your app from what I am seeing...