jpush-react-native icon indicating copy to clipboard operation
jpush-react-native copied to clipboard

clearNotificationById报错Value for nitificationId cannot be cast from String to Double

Open inkCrazy opened this issue 1 year ago • 2 comments

你的运行环境

  • 插件版本: "jcore-react-native": "^2.1.9", "jpush-react-native": "^3.0.9",
  • react-native 版本: "react": "18.2.0", "react-native": "0.72.4",

期望效果

处理消息完毕之后,把通知栏目对应的消息去掉

实际效果

报错:Value for nitificationId cannot be cast from String to Double

消息过来

重现步骤

 监听了addNotificationListener方法
  this.notificationListener = result => {
    console.log('notificationListener:' + JSON.stringify(result));
    console.log('notificationListener:', result);
    if (
      result &&
      result.extras &&
      result.notificationEventType === 'notificationArrived'
    ) {
      let extras = result.extras;
      switch (extras.type) {
        case 'sensitive':
          console.log('授权通知', extras);
          this.setState(
            {
              authInfo: {
                ...extras,
                isVisible_out: true,
              },
            },
            () => {
              // JPush.clearAllNotifications();
              console.log('JPush.clearNotificationById', result.messageID);
              //报错:Value for nitificationId cannot be cast from String to Double
              JPush.clearNotificationById({notificationId: result.messageID});
            },
          );
          break;
        default:
          break;
      }
    }
  };
  JPush.addNotificationListener(this.notificationListener);

Debug logs

包括 Android 或 iOS 的日志: //报错:Value for nitificationId cannot be cast from String to Double JPush.clearNotificationById({notificationId: result.messageID});

inkCrazy avatar Aug 20 '24 08:08 inkCrazy

一样报错

xb18 avatar Jan 01 '25 15:01 xb18

这是因为JPush.clearNotificationById 接收的参数并不是 messageID,而是notificationId,但是 notificationId 在java 接收后并未传递到react native,所以需要手动修改一下源码:

找到 cn.jiguang.plugins.push.helper.JPushHelper#convertNotificationToMap 方法 [jpush-react-native/android/src/main/java/cn/jiguang/plugins/push/helper/JPushHelper.java]

在传入的参数中增加notificationId

public static WritableMap convertNotificationToMap(String eventType, NotificationMessage message) {
        WritableMap writableMap = Arguments.createMap();
        writableMap.putString(JConstants.NOTIFICATION_EVENT_TYPE, eventType);
        writableMap.putString(JConstants.MESSAGE_ID, message.msgId);
        writableMap.putString(JConstants.TITLE, message.notificationTitle);
        writableMap.putString(JConstants.CONTENT, message.notificationContent);
        writableMap.putInt(JConstants.NOTIFICATION_ID, message.notificationId);  // 新增notificationId
        convertExtras(message.notificationExtras, writableMap);
        return writableMap;
    }

在rn 中使用notificationId

JPush.clearNotificationById({notificationId: result.notificationId});

notificationId 使用int 类型,不然还会出现类型转换错误,原因是:

    @ReactMethod
    public void clearNotificationById(ReadableMap readableMap){
        if (readableMap == null){
            JLogger.w(JConstants.PARAMS_NULL);
            return;
        }
        if (readableMap.hasKey(JConstants.NOTIFICATION_ID)){
            Integer id = readableMap.getInt(JConstants.NOTIFICATION_ID); // 获取 int 类型
            JPushInterface.clearNotificationById(reactContext,id);
        }else {
            JLogger.w("there are no " + JConstants.GEO_FENCE_ID);
        }
    }

这个问题2017年就已经存在,见 https://community.jiguang.cn/question/194750?fs=1

Maasea avatar Sep 25 '25 07:09 Maasea