ios - 推送通知的图标?

标签 ios xcode push-notification

我正在开发一个接收推送通知的聊天应用程序。我想提供 iOS 用来在推送通知中显示的图像/图标,我可以在 Xcode 中的什么地方指定它?

最佳答案

要完成此操作,您首先需要转到您的 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 send a key -value 对代码中我们使用键 'newIcon' 所以像那样发送它

{"newIcon":"first_icon"}

现在当您发送通知时会显示 iconName。

这会起作用..

关于ios - 推送通知的图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42719187/

相关文章:

ios - UIModalTransitionStylePartialCurl 导致 UIButton 文本动画

ios - Xcode 4.5 安装在我的 Mac 上的什么位置?

push-notification - 跨主要浏览器和移动设备发送和接收推送通知

ios - 在应用程序处于终止状态时增加或减少应用程序角标(Badge)

ios - AVPlayerViewController PIP...如何返回应用程序?

iphone - 主要/默认 ABMultiValue 属性?

ios - Realm `description` 属性与其他属性的行为不同

java - Xcode 中的 "Add unimplemented methods"类似于 Eclipse

ios - 如何避免索引超出范围的错误?

android - 你能在 Windows 服务器上设置 GCM 吗?