ios 小米消息推送

本人前端,最近弄RN项目,需要使用小米的消息推送服务,代码可以打印出regId和注册的deviceToken信息。但是没有执行回调miPushReceiveNotification函数,因为没有打印出对应的日志。具体代码:

/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#import "AppDelegate.h"
#import "MiPushSDK.h"
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  NSURL *jsCodeLocation;

  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"friday"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  
  NSLog(@"===**&&**====%@", [MiPushSDK getRegId]);
  /**
   *  小米推送,同时启用APNs和应用内长链接
   */
  [MiPushSDK registerMiPush:self type:0 connect:YES];
  
  // 处理点击通知打开app的逻辑
  NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
  if(userInfo){//推送信息
    NSString *messageId = [userInfo objectForKey:@"_id_"];
    if (messageId!=nil) {
      [MiPushSDK openAppNotify:messageId];
    }
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"gg" message:[NSString stringWithFormat:@"%@", userInfo] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *act = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
      
    }];
    [alert addAction:act];
  }
  
  return YES;
}
- (void)application:(UIApplication *)app
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  // 注册APNS成功, 注册deviceToken
  [MiPushSDK bindDeviceToken:deviceToken];
  
  NSLog(@"注册成功------");
  NSLog(@"注册信息 = %@", deviceToken);
}

- (void)application:(UIApplication *)app
didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
{
  // 注册APNS失败
  // 自行处理
  NSLog(@"注册失败------");
}

- (void)miPushRequestSuccWithSelector:(NSString *)selector data:(NSDictionary *)data
{
  // 请求成功
  // 可在此获取regId
  NSLog(@"请求成功------");
  if ([selector isEqualToString:@"bindDeviceToken:"]) {
    NSLog(@"regid = %@", data[@"regid"]);
  }
}
- (void)miPushRequestErrWithSelector:(NSString *)selector error:(int)error data:(NSDictionary *)data
{
  // 请求失败
  NSLog(@"请求失败------");
}

- ( void )miPushReceiveNotification:( NSDictionary *)data
{
  // 长连接收到的消息。消息格式跟APNs格式一样
//  NSLog(@"msg***= %@", data);
  NSLog(@"收到消息------");
  NSLog(@"what is this = %@", data);
}

- ( void )application:( UIApplication *)application didReceiveRemoteNotification:( NSDictionary *)userInfo
{
  [ MiPushSDK handleReceiveRemoteNotification :userInfo];
  // 使用此方法后,所有消息会进行去重,然后通过miPushReceiveNotification:回调返回给App
  NSString *messageId = [userInfo objectForKey:@"_id_"];
  [MiPushSDK openAppNotify:messageId];
  NSLog(@"收到消息-222-----");
}

// iOS10新加入的回调方法
// 应用在前台收到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  NSDictionary * userInfo = notification.request.content.userInfo;
  NSLog(@"iOS10新加入的回调方法------");
  if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    [MiPushSDK handleReceiveRemoteNotification:userInfo];
  }
  //    completionHandler(UNNotificationPresentationOptionAlert);
}

// 点击通知进入应用
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
  NSDictionary * userInfo = response.notification.request.content.userInfo;
  if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    [MiPushSDK handleReceiveRemoteNotification:userInfo];
  }
  completionHandler();
}



@end

麻烦各位看一下,哪里有问题吗?

阅读 2.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题