6

I am working on a chat app that receive push notification. I want to provide images/icon that will be used by iOS to display in push notification , where can I specify that in Xcode?

Sagar V
  • 12,158
  • 7
  • 41
  • 68
Aashish Nagar
  • 1,207
  • 1
  • 14
  • 30
  • 1
    You cannot AFAIK. It will use the icon of your app. You can read more here : https://developer.apple.com/ios/human-interface-guidelines/features/notifications/ – Losiowaty Mar 10 '17 at 13:20
  • 1
    Sure you can, by using Images.xcassets. Just insert the image you want to the field "iPhone Notification" / "iPad Notification" – Fabio Berger Mar 10 '17 at 14:32
  • 1
    You should take a look at this: http://stackoverflow.com/questions/37839171/how-to-display-image-in-ios-push-notification – Milander Mar 10 '17 at 14:57
  • @Losiowaty since iOS 10, you **can**. Just see the link Milander has shared... – mfaani Jun 10 '17 at 08:25
  • there is also a very very good WWDC video related. See from [this moment](https://developer.apple.com/videos/play/wwdc2016/708/?time=444) – mfaani Jun 10 '17 at 08:41
  • Possible duplicate of [how to display image in ios push notification?](https://stackoverflow.com/questions/37839171/how-to-display-image-in-ios-push-notification) – Deepraj Chowrasia Oct 31 '17 at 06:44

1 Answers1

0

To Accomplish this firstly you need to goto your Info.Plist file and add some properties in Icon-File that is

<key>CFBundleIcons</key>
    <dict>
        <key>CFBundleAlternateIcon</key>
        <dict>
            <key>first_icon</key>
            <dict>
                <key>CFBundleIconFile</key>
                <array>
                    <string>first_icon.png</string>
                </array>
            </dict>
            <key>second_icon</key>
            <dict>
                <key>CFBundleIconFile</key>
                <array>
                    <string>second_icon.png</string>
                </array>
            </dict>
        </dict>
        <key>CFBundlePrimaryIcon</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string></string>
            </array>
            <key>UIPrerenderedIcon</key>
            <false/>
        </dict>
        <key>UINewsstandIcon</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string></string>
            </array>
            <key>UINewsstandBindingType</key>
            <string>UINewsstandBindingTypeMagazine</string>
            <key>UINewsstandBindingEdge</key>
            <string>UINewsstandBindingEdgeLeft</string>
        </dict>
    </dict>

Now you need to configure settings in your AppDelegate file

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{

      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        NSDictionary *notificationData = [[PushNotificationManager pushManager] getCustomPushDataAsNSDict:userInfo];
        NSString * notiIcon = [notificationData objectForKey:@"newIcon"];
        if([notiIcon isEqualToString:@"nil"])
          notiIcon = nil;

        NSLog(@"icon is: %@", notiIcon);

        [[UIApplication sharedApplication] setAlternateIconName:notiIcon completionHandler:^(NSError * _Nullable error) {
          NSLog(@"Set icon error = %@", error.localizedDescription);
        }];
      });

now from any dashboard you send the notification to the app goto that and there would be and option named Action or something like send Custom data send a key-value pair in code we are using key 'newIcon' so send it like that

{"newIcon":"first_icon"}

now when you'll send the notification with iconName that will appear.

This will Work..

cpt. Sparrow
  • 415
  • 8
  • 22
  • @Aashish Nagar I have changed my answer my previous answer was removed because of some copy right issues check this one now – cpt. Sparrow Nov 03 '17 at 02:53