2017-03-10 77 views
7

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

+1

你不能AFAIK。它将使用您的应用程序的图标。你可以在这里阅读更多:https://developer.apple.com/ios/human-interface-guidelines/features/notifications/ – Losiowaty

+1

当然你可以通过使用Images.xcassets。 只需将你想要的图像插入字段“iPhone通知”/“iPad通知” –

+1

你应该看看这个:http://stackoverflow.com/questions/37839171/how-to-display-image-in- ios-push-notification – Milander

回答

0

要做到这一点,首先你需要到您的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); 
     }]; 
     }); 

现在从任何仪表板你发送通知到应用程序goto,并会有和选项名称Action或类似的东西send Custom data发送key-value对我们正在使用的密钥'newIcon'所以发送它像这样

{"newIcon":"first_icon"} 

现在当你发送带有iconName的通知将出现。

这将工作..

+0

@Aashish Nagar我改变了我的答案我以前的答案被删除,因为一些版权问题现在检查这一个 –