-1

I am working on an app in which I have implemented Apple push notifications. When my app is in background state then I am able to receive push notifications without sound but when my app is in active state then I am able to get sound for notifications but not showing any banner for it.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    UIApplicationState state = [application applicationState];

    if (state == UIApplicationStateInactive)
    {
        AudioServicesPlaySystemSound(1007);
    }
    else {
        AudioServicesPlaySystemSound(1007);

        carPlateNo = [userInfo valueForKeyPath:@"aps.alert.loc-args"];
        if(![carPlateNo isEqualToString:@"Return Message"])
        {
            [self receiveServices];
        }
        // Push Notification received in the background
    }
}

Can anyone help on this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579

4 Answers4

4

You have to set sound in APNS Payload only, and add sound clip in XCode bundle.

Sample Payload

{
    "aps" : {
        "alert" : "You got your emails.",
        "badge" : 9,
        "sound" : "bingbong.aiff"
    },
    "acme1" : "bar",
    "acme2" : 42
}

More Info

Bhavesh Dhaduk
  • 1,888
  • 15
  • 30
  • You're welcome..... :) you should accept correct answer as per stackoverflow guideline so that other people can get help having same issue! – Bhavesh Dhaduk Jul 13 '17 at 13:24
1

Try This :

when you send push notification, just add the name of sound in JSON payload. Example:

{
   "aps" : {
    "alert" : "your alert message here.",
    "badge" : 1,
    "sound" : "samplemusic.aiff"
   }
}

Thats it! You don't have to do anything special in code of app.

Soumya Ranjan
  • 4,817
  • 2
  • 26
  • 51
0

iOS takes care of delivering notification to app when app is killed. iOS also takes care of showing the banner/alert depending on user settings and also plays the audio for the notification.

What you can do though is specify what audio to play in your notification payload

{
    "aps" : {
        "alert" : "You got your emails.",
        "badge" : 9,
        "sound" : "bingbong.aiff"
    },
    "acme1" : "bar",
    "acme2" : 42
}

If you don't want default notification sound you can bundle your own audio file of format .aiff and ship it with app and specify the file name in your notification payload as shown above

refer : https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html

As far as

when my app is in active state then I am able to get sound for notifications but not showing any banner for it

concerned, when your app is in foreground its your app's responsibility to show any custom banner/alert. System default banner/alert will not be displayed. You can use https://github.com/sandeeplearner/SBNotificationBar for the same :)

Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
0

- When my app is in background state then I am able to receive push notifications without sound

There must be one the following two issues

1.Sound key is not there or sending empty value for it in payload (Reference)

2.Your phone in silent mode

- my app is in active state then I am able to get sound for notifications but not showing any banner for it.

When Application is active app is responsible to handle what to do on push notification receive for more please refer this answer

Narayana Rao Routhu
  • 6,303
  • 27
  • 42