fix: 修复iOS收不到推送问题

This commit is contained in:
“DaisyWu” 2025-02-13 10:56:24 +08:00
parent 044990cad5
commit 2f41195d8b
5 changed files with 97 additions and 14 deletions

View File

@ -9,6 +9,7 @@
@interface AppDelegate()
@property (nonatomic, strong) FlutterMethodChannel *methodChannel;
@end
@ -20,11 +21,24 @@
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// [UMConfigure initWithAppkey:@"671244ae80464b33f6df9646" channel:@"Product"];
//Required
//notice: 3.0.0
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
// categories
// NSSet<UNNotificationCategory *> *categories for iOS10 or later
// NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9
}
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
XSFlutterManager *VC = [[XSFlutterManager alloc] init];
self.window.rootViewController = VC;
[self.window makeKeyAndVisible];
return YES;
}
@ -64,7 +78,7 @@
*/
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
//sdkDeviceToken
/// Required - DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
@ -72,7 +86,8 @@
*
*/
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"didFailToRegisterForRemoteNotificationsWithError %@", error);
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
/**
@ -96,6 +111,49 @@
NSLog(@"Receive message title: %@, content: %@.", title, body);
}
# pragma mark- JPUSHRegisterDelegate
// iOS 12 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{
if (notification && [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//
}else{
//
}
}
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
// Required
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler(UNNotificationPresentationOptionAlert); // BadgeSoundAlert
}
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
// Required
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler(); //
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Required, iOS 7 Support
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// Required, For systems with less than or equal to iOS 6
[JPUSHService handleRemoteNotification:userInfo];
}

View File

@ -64,6 +64,7 @@
activityVC.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
if (completed) {
NSLog(@"completed");
//
} else {
NSLog(@"canceled");
@ -72,6 +73,10 @@
};
result(@"push返回到flutter");
}
if ([method isEqualToString:@"getBundleIdentifier"]) {
NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
result(bundleIdentifier); // Bundle Identifier
}
}];
[GeneratedPluginRegistrant registerWithRegistry:self];

View File

@ -14,7 +14,6 @@ import 'package:star_lock/talk/starChart/proto/gateway_reset.pb.dart';
import 'package:star_lock/talk/starChart/proto/generic.pb.dart';
import 'package:star_lock/talk/starChart/proto/talk_expect.pb.dart';
import 'package:star_lock/talk/starChart/proto/talk_request.pb.dart';
import 'package:star_lock/tools/push/xs_jPhush.dart';
import 'package:star_lock/tools/storage.dart';
class UdpTalkRequestHandler extends ScpMessageBaseHandle
@ -79,7 +78,9 @@ class UdpTalkRequestHandler extends ScpMessageBaseHandle
//
Future<void> _showTalkRequestNotification(
{required String talkObjectName}) async {
final Map<String, dynamic> message = {
/**
*
final Map<String, dynamic> message = {
'platform': 'all',
'audience': 'all',
'notification': <String, Map<String, Object>>{
@ -95,8 +96,7 @@ class UdpTalkRequestHandler extends ScpMessageBaseHandle
};
XSJPushProvider().showCustomNotification(message);
/*
*/
const AndroidNotificationDetails androidPlatformChannelSpecifics =
AndroidNotificationDetails('1', 'flutter_channel',
importance: Importance.max,
@ -113,7 +113,6 @@ class UdpTalkRequestHandler extends ScpMessageBaseHandle
await flutterLocalNotificationsPlugin.show(0, '呼叫提醒'.tr,
'${'收到来自'.tr}($talkObjectName)${'锁的呼叫'.tr}', platformChannelSpecifics,
payload: 'item x');
*/
}
@override

View File

@ -1,5 +1,6 @@
import 'package:flutter/services.dart';
import 'package:star_lock/flavors.dart';
import 'package:star_lock/tools/push/xs_jPhush.dart';
import '../app_settings/app_settings.dart';
@ -52,4 +53,15 @@ class NativeInteractionTool {
}
});
}
Future<String?> getBundleIdentifier() async {
try {
final String? bundleIdentifier =
await sendChannel.invokeMethod('getBundleIdentifier');
return bundleIdentifier;
} on PlatformException catch (e) {
print("Failed to get bundle identifier: '${e.message}'.");
return null;
}
}
}

View File

@ -1,11 +1,13 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
import 'package:star_lock/flavors.dart';
import 'package:star_lock/mine/minePersonInfo/minePersonInfoEditAccount/minePersonInfoEditAccount/mineUnbindPhoneOrEmail_entity.dart';
import 'package:star_lock/network/api_repository.dart';
import 'package:star_lock/tools/NativeInteractionTool.dart';
import 'package:star_lock/tools/baseGetXController.dart';
import 'package:star_lock/tools/push/message_management.dart';
import 'package:star_lock/tools/push/notification_service.dart';
@ -15,7 +17,6 @@ import '../../app_settings/app_settings.dart';
class XSJPushProvider {
final JPush jpush = JPush();
final NotificationService _notificationService = NotificationService();
// appKey: 251fc8074820d122b6de58d2--AppKey
// appKey: 7ff37d174c1a568a89e98dad--sky
@ -27,6 +28,9 @@ class XSJPushProvider {
}
print('jPushKey ${F.jPushKey}');
// final String? bundleIdentifier =
// await NativeInteractionTool().getBundleIdentifier();
// print('bundleIdentifier: $bundleIdentifier');
jpush.setup(
appKey: F.jPushKey,
channel: 'flutter_channel',
@ -40,6 +44,10 @@ class XSJPushProvider {
addJPushEventHandler();
AppLog.log('JPush initialized.');
final String rid = await jpush.getRegistrationID();
print('flutter get registration id : $rid');
pushBindDeviceID(rid);
}
//
@ -69,11 +77,13 @@ class XSJPushProvider {
final String rid = await jpush.getRegistrationID();
AppLog.log('onConnected registration id : $rid');
await Storage.setString(pushDeviceID, rid);
await pushBindDeviceID(rid, Platform.isAndroid ? 10 : 20);
await pushBindDeviceID(rid);
return Future.value();
},
);
// Remove the incorrect addEventHandler call
}
Future<void> showCustomNotification(Map<String, dynamic> message) async {
@ -86,7 +96,7 @@ class XSJPushProvider {
NotificationService().showImageNotification(title, content, imageUrl);
}
Future<void> pushBindDeviceID(String deviceID, int deviceType) async {
Future<void> pushBindDeviceID(String deviceID) async {
try {
if (deviceID.isEmpty) {
AppLog.log('Device ID is empty.');
@ -95,10 +105,9 @@ class XSJPushProvider {
AppLog.log('onConnected registration id : $rid');
await Storage.setString(pushDeviceID, rid);
deviceID = rid;
deviceType = Platform.isAndroid ? 10 : 20;
}
final MineUnbindPhoneOrEmailEntity entity =
await ApiRepository.to.pushBindAppId(deviceID, deviceType);
final MineUnbindPhoneOrEmailEntity entity = await ApiRepository.to
.pushBindAppId(deviceID, Platform.isAndroid ? 10 : 20);
if (entity.errorCode!.codeIsSuccessful) {
AppLog.log('绑定成功');
} else {