import PushNotificationIOS from "@react-native-community/push-notification-ios";
import PushNotification, { Importance } from "react-native-push-notification";

global.notifyToken = {};
// Must be outside of any component LifeCycle (such as `componentDidMount`).
PushNotification.configure({
  //（可选）生成Token时调用（iOS和Android）
  onRegister: function (token) {
    console.log("TOKEN:", token);
    global.notifyToken = token;
  },
  // （必需）在收到或打开远程通知或打开本地通知时调用
  onNotification: function (notification) {
    console.log("NOTIFICATION:", notification);

    if (notification.title) {
      //TODO 处理通知
      notification.channelId = "10186";
      notification.smallIcon = "ic_notification";
      notification.showWhen = true;
      PushNotification.localNotification(notification);
    } else if (notification.data) {
      //TODO 处理消息
      
    }

    //（必需）在收到或打开远程通知或打开本地通知时调用
    if (notification.finish) {
      notification.finish(PushNotificationIOS.FetchResult.NoData);
    }
  },

  // （可选）当按下Registered Action且invokeApp为false时调用，如果为true，将调用onNotification（Android）
  onAction: function (notification) {
    console.log("ACTION:", notification.action);
    console.log("NOTIFICATION:", notification);

    // process the action
  },

  //（可选）当用户注册远程通知失败时调用。通常发生在APN出现问题时，或者设备是模拟器时(操作系统）
  onRegistrationError: function(err) {
    console.info(err.message, err);
  },

  // 仅限IOS（可选）：默认值：all - Permissions to register.
  permissions: {
    alert: true,
    badge: true,
    sound: true,
  },

  // 初始通知自动弹出
  // default: true
  popInitialNotification: true,

  /**
   *（可选）默认值：true
   *-指定是否请求权限（ios）和令牌（android和ios），
   *-否则，必须稍后调用PushNotificationsHandler.requestPermissions（）
   *-如果未使用远程通知或未安装Firebase，请使用以下命令：
   * requestPermissions:Platform.OS==='ios'
   */
  requestPermissions: true,
});

PushNotification.createChannel(
  {
    channelId: "10186", // (required)
    channelName: "Application Messaging", // (required)
    channelDescription: "Application notification center, remind of the charging process", // (optional) default: undefined.
    playSound: false, // (optional) default: true
    soundName: "default", // (optional) See `soundName` parameter of `localNotification` function
    importance: Importance.HIGH, // (optional) default: Importance.HIGH. Int value of the Android notification importance
    vibrate: true, // (optional) default: true. Creates the default vibration pattern if true.
  },
  //(created) => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
);

/*PushNotification.getChannels(function (channel_ids) {
  console.log('channel_ids', channel_ids); // ['channel_id_1']
});*/