推送通知的图标?

时间:2017-03-10 13:08:57

标签: ios xcode push-notification

我正在开发一个接收推送通知的聊天应用。我想提供iOS将用于在推送通知中显示的图像/图标,在哪里可以在Xcode中指定?

1 个答案:

答案 0 :(得分:0)

要首先完成此操作,您需要转到Info.Plist文件并在Icon-File中添加一些属性

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

现在您需要在AppDelegate文件中配置设置

    - (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);
        }];
      });

现在,您可以从任何信息中心向应用程序发送通知,并且会出现名为Action的选项或类似send Custom data的内容,在我们使用密钥的代码中发送key-value对&#39; newIcon&#39;所以像那样发送

{"newIcon":"first_icon"}

现在,当您发送带有将出现的iconName的通知时。

这将有效..