简体   繁体   中英

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

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.

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);

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];

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 ..,

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"
      }

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