Skip to content

IOS无法收到通知,Android正常 #510

@GraveSoil

Description

@GraveSoil

import 'package:flutter/material.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
import 'package:jpush_flutter/jpush_interface.dart';
import 'package:permission_handler/permission_handler.dart';
import 'dart:async';

class JPushManager {
static final _jpush = JPush.newJPush();
static bool _initialized = false;

/// 初始化 JPush(建议在 main() 中调用)
static Future init({
required String appKey,
String channel = "default",
bool production = false,
bool debug = false,
}) async {
if (_initialized) return;

// 设置 SDK
_jpush.setup(
  appKey: appKey,
  channel: channel,
  production: production,
  debug: debug,
);

_jpush.applyPushAuthority(
    const NotificationSettingsIOS(
        sound: true,
        alert: true,
        badge: true
    )
);

// 注册事件回调
_registerEventHandlers();

_initialized = true;

}

/// 注册所有事件监听
static void _registerEventHandlers() {

_jpush.addEventHandler(
  // 收到通知(无论前后台)
    onReceiveNotification: (Map<String, dynamic> message) async {
      debugPrint("[JPush] onReceiveNotification: $message");
      // 通常不需要在这里 show 通知,因为系统已自动显示(APNs)
    },
    // 用户点击通知
    onOpenNotification: (Map<String, dynamic> message) async {
      debugPrint("[JPush] onOpenNotification: $message");
      _handleNotificationClick(message);
    },
    // 收到自定义消息(透传消息,不弹通知)
    onReceiveMessage: (Map<String, dynamic> message) async {
      debugPrint("[JPush] onReceiveMessage: $message");
      // 可用于静默数据同步

    },
    // 通知权限变更
    onReceiveNotificationAuthorization:
        (Map<String, dynamic> message) async {
      debugPrint("[JPush] onReceiveNotificationAuthorization: $message");

    });

}

/// 处理通知点击逻辑(可自行扩展)
static void _handleNotificationClick(Map<String, dynamic> message) {

}

// ------------------ 公共方法 ------------------

/// 获取 Registration ID(用于服务器推送指定设备)
static Future<String?> getRegistrationId() async {
return await _jpush.getRegistrationID();
}

/// 发送本地通知(仅当前设备)
static Future sendLocalNotification({
required int id,
required String title,
required String content,
DateTime? fireTime,
String? subtitle,
int? badge,
Map<String, String>? extra,
}) async {
fireTime ??= DateTime.now().add(const Duration(microseconds: 200));
final notification = LocalNotification(
id: id,
title: title,
buildId: 1,
content: content,
fireTime: null,
subtitle: subtitle,
badge: badge,
extra: extra,
);
await _jpush.sendLocalNotification(notification);
}

/// 设置角标
static Future setBadge(int count) async {
await _jpush.setBadge(count);
}

/// 清除所有通知
static Future clearAllNotifications() async {
await _jpush.clearLocalNotifications();
await _jpush.clearAllNotifications();
}

/// 检查通知权限是否开启
static Future isNotificationEnabled() async {
return await _jpush.isNotificationEnabled();
}

/// 打开系统通知设置页
static void openNotificationSettings() {
_jpush.openSettingsForNotification();
}

/// 设置别名(用于定向推送)
static Future setAlias(String alias) async {
await _jpush.setAlias(alias);
}

/// 设置标签
static Future setTags(List tags) async {
await _jpush.setTags(tags);
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions