推送通知的图标?

6

我正在开发一个接收推送通知的聊天应用。我想提供用于显示在推送通知中的图片/图标,那么在 Xcode 中该如何指定呢?


1
据我所知,你不能这样做。它将使用你的应用程序图标。你可以在这里阅读更多信息:https://developer.apple.com/ios/human-interface-guidelines/features/notifications/ - Losiowaty
1
当然可以,通过使用Images.xcassets。只需将所需的图像插入到“iPhone通知”/“iPad通知”字段中即可。 - Fabio Berger
1
你应该看一下这个:https://dev59.com/dFoU5IYBdhLWcg3wFEE6 - Milander
自iOS 10以来,你是可以的。只需查看Milander分享的链接... - mfaani
还有一段非常非常好的与WWDC相关的视频。请从这个时刻开始观看。 - mfaani
可能是如何在iOS推送通知中显示图像?的重复问题。 - Deepraj Chowrasia
1个回答

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对的形式发送代码中使用键'newIcon',因此像这样发送它

{"newIcon":"first_icon"}

现在当您发送带有图标名称的通知时,它将显示。

这将起作用。


@Aashish Nagar,我已经修改了我的回答。之前的回答因为版权问题被删除了,请现在查看这个新的回答。 - cpt. Sparrow

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接