Sha*_*que 5 ios testflight react-native onesignal react-native-onesignal
我按照本指南在我的反应本机应用程序中设置 OneSignal 后台通知:https ://documentation.onesignal.com/docs/rn-android-native-module-setup-for-notification-service-extension#ios-notification-服务扩展模块
当我通过 Xcode 将应用程序直接安装到我的设备时,后台通知按预期工作。但是当我存档构建并从 TestFlight 安装它时,后台通知不起作用。除非emitNotificationEvent
我添加的事件没有被触发,即使收到了推送通知。
当我在 Xcode 中跟踪存档构建的问题(使用设备控制台)时,_instance
注意到NotificationExtensionModule.m
. 有人遇到过类似的问题或知道可能是什么原因吗?
笔记
添加代码片段以进一步了解我的问题:
AppDelegate.h
#import <Foundation/Foundation.h>
#import <React/RCTBridgeDelegate.h>
#import <UIKit/UIKit.h>
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,RCTBridgeDelegate,UNUserNotificationCenterDelegate>
@property (nonatomic, strong) UIWindow *window;
@end
Run Code Online (Sandbox Code Playgroud)
AppDelegate.m
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
//access NotificationServiceExtensionModule emitNotificationEvent method
[NotificationServiceExtensionModule.sharedInstance emitNotificationEvent:userInfo ];
completionHandler(UIBackgroundFetchResultNoData);
}
Run Code Online (Sandbox Code Playgroud)
通知服务扩展模块.h
#import <foundation/Foundation.h>
// NotificationServiceExtensionModule.h
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
@interface NotificationServiceExtensionModule : RCTEventEmitter <RCTBridgeModule>
+ (NotificationServiceExtensionModule*) sharedInstance;
- (void)emitNotificationEvent:(NSDictionary *)userInfo;
@end
Run Code Online (Sandbox Code Playgroud)
通知服务扩展模块.m
#import <Foundation/Foundation.h>
// NotificationServiceExtensionModule.m
#import "NotificationServiceExtensionModule.h"
@implementation NotificationServiceExtensionModule
static NotificationServiceExtensionModule* _instance = nil;
+(NotificationServiceExtensionModule*) sharedInstance {
// @synchronized( _instance ) {
// if( !_instance ) {
// _instance = [[NotificationServiceExtensionModule alloc] init];
// }
// }
return _instance; // this returns null when installed from TestFlight.
}
// To export a module named NotificationServiceExtensionModule
RCT_EXPORT_MODULE();
- (NSArray<NSString *> *)supportedEvents
{
NSLog(@"Supported EVENTS__________________________");
_instance = self;
return @[@"NotificationEvent"];
}
- (void)emitNotificationEvent:(NSDictionary *)userInfo
{
NSString *eventName = userInfo[@"custom"][@"a"];
[self sendEventWithName:@"NotificationEvent" body:@{@"notificationPayload": eventName}];
}
@end
Run Code Online (Sandbox Code Playgroud)
您可以使用以下代码来检查应用程序是否在后台或前台运行:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
//access NotificationServiceExtensionModule emitNotificationEvent method
[NotificationServiceExtensionModule.sharedInstance emitNotificationEvent:userInfo ];
if (application.applicationState == UIApplicationStateActive) {
// App was in the foreground when the notification was received
} else {
// App was in the background when the notification was received
}
completionHandler(UIBackgroundFetchResultNoData);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
791 次 |
最近记录: |