【问题标题】:Icon for push notification?推送通知的图标?
【发布时间】:2017-07-31 20:17:42
【问题描述】:

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

【问题讨论】:

标签: ios xcode push-notification


【解决方案1】:

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

<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 的选项在我们使用密钥“newIcon”的代码中发送 key-value 对,所以发送它像这样

{"newIcon":"first_icon"}

现在当您发送通知时会出现图标名称。

这会起作用..

【讨论】:

  • @Aashish Nagar 我已经改变了我的答案,因为一些版权问题,我之前的答案被删除了现在检查这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多