0

I'm working on an iOS chat application which is in a UIWebView. I have implemented push notifications as well. But I want to know how to detect if a chat message(push notification) has been received on a device and be able to set a badge icon accordingly.

Shamas S
  • 7,507
  • 10
  • 46
  • 58
  • I can help you with a simple logic... When, you receive msg in chat.. call for a push notification indicating message is delivered and when u tap on message and open that screen, call another push notification indicating message has been read.. – Tejvansh Apr 09 '15 at 07:13
  • Your main problem is to how to set the badge icon when a push notification is received. That is why I updated the title as well. Have a good day! – Shamas S Apr 09 '15 at 10:33

1 Answers1

0

Using parse.com (default for Push Notifications) you can "send a badge" by setting the "badge" key to whatever number you want. The operating system on the receiving end will then set the badge number for you. See below:

    PFPush *push = [[PFPush alloc] init];
    NSString *userChannel = [NSString stringWithFormat:@"USER-%@", userId];
    [push setChannel:userChannel];
    NSDictionary *data = @{@"alert":@"You have a new review!",@"badge":@1, @"sound":@"default"};
    [push setData:data];
    [push sendPushInBackground];

As of iOS 8, note you must also include the "sound" key if you want the receiver to hear a chime upon reception of push notification.

On the receiving end, you will likely want to clear the badge icon once the user has opened the app. You do that as follows:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    [PFPush handlePush:userInfo];
}
etayluz
  • 15,920
  • 23
  • 106
  • 151
  • How can we achieve this without using Parse..is there any way? I already implemeted the push notifications in my app, but havent did the badge icon. I need to detect when a message is recieved on my device(chat app) and if the app is not opened i want to show the badge icon..any ideas? – Sachit Philip Apr 10 '15 at 07:18
  • What did you use for push notifications? How did you implement it? – etayluz Apr 10 '15 at 07:48
  • Im using the default apple push notifications with backend support – Sachit Philip Apr 10 '15 at 09:12