idoodler
idoodler

Reputation: 3545

How to play sound when receiving a Push notification in iPhone

Hi I am trying to play the default Push sound when receiving a Push Notification on my iDevice I used this code to play the sound in the

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo` Method 

NSDictionary *test =(NSDictionary *)[userInfo objectForKey:@"aps"];
    NSString *alertString =(NSString *) [test objectForKey:@"alert"];
    NSLog(@"String recieved: %@",alertString);
    if (state == UIApplicationStateActive) {
            UIAlertView *alertmessage=[[UIAlertView alloc]initWithTitle:@"iEverything Tech"
                                                                message:alertString                                                    delegate:self
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil];


            [alertmessage show];

            AudioServicesPlaySystemSound(1002);


        }

        if (state == UIApplicationStateInactive) {
            AudioServicesPlaySystemSound(1002);
        }

        if (state == UIApplicationStateBackground) {
            AudioServicesPlaySystemSound(1002);
        }

And my second question is how to show the Pushed message in the AlertView?

Thank you for your answers!

And I can't use a Push provider like Parse because we hour own server and we need to push automatically

Upvotes: 3

Views: 12786

Answers (6)

Ravi D
Ravi D

Reputation: 863

Try this to retrieve the notification message and "alertString" below holds the message received

NSDictionary *test =(NSDictionary *)[userInfo objectForKey:@"aps"];
NSString *alertString =(NSString *) [test objectForKey:@"alert"];
NSLog(@"String recieved: %@",alertString);

Upvotes: 1

Tarika Chawla
Tarika Chawla

Reputation: 133

In order to play a default sound for the notifications in iOS, you need to add the following code to the payload json

"sound" : "default"

So, you "notification" payload should look something like:

 "notification": {
        "title": "4x8",
        "body": "15:16.2342",
        "message":"https://www.google.com",
        "sound" : "default"
      }

Upvotes: 0

Ravi D
Ravi D

Reputation: 863

In your "application didfinishlaunchingwithoptions add the following

NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif) {
    [self handleRemoteNotification:application userInfo:remoteNotif];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    [UIApplication sharedApplication].applicationIconBadgeNumber--;        
}

When you open the app through remote notification, by tapping on the notification this will decrease the badge number, if you want to remove the badge number when ever user opens the app then just implement the code under if condition, if condition here just checks if the application has been opened through by tapping on remote notifications ..,

Upvotes: 0

Ravi D
Ravi D

Reputation: 863

Simply pass the string into the alert that will do

NSDictionary *test =(NSDictionary *)[userInfo objectForKey:@"aps"];
NSString *alertString =(NSString *) [test objectForKey:@"alert"];
NSLog(@"String recieved: %@",alertString);
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Title" message:alertString delegate:self cancelButtonTitle:@"OK" otherButtonTitles: @"Not OK", nil] autorelease];
[alert show];

Upvotes: 0

jimpic
jimpic

Reputation: 5520

Like NSEncoder wrote, the sound has to be in the notification payload. To answer the second question, your notification will either be displayed in an alert, badge or not at all - depending on the setting in the users notification settings, you have no influence on this.

Upvotes: 2

Related Questions