我正在开发一个接收推送通知的聊天应用程序。我想提供 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/