[英]React Native OneSignal Background Notification Issue in iOS
我按照本指南在我的本機應用程序中設置 OneSignal 后台通知: https://documentation.onesignal.com/docs/rn-android-native-module-setup-for-notification-service-extension#ios-notification-服務擴展模塊
當我通過 Xcode 將應用程序直接安裝到我的設備時,后台通知按預期工作。但是當我存檔構建並從 TestFlight 安裝時,后台通知不起作用。 除非我添加的emitNotificationEvent
事件沒有被觸發,即使收到推送通知也是如此。
當我在 Xcode(使用設備控制台)中跟蹤存檔構建的問題時,注意到NotificationExtensionModule.m
中的_instance
是 null。 任何人都遇到過類似的問題或知道可能是什么原因?
筆記
添加代碼片段以進一步了解我的問題:
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
AppDelegate.m
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
//access NotificationServiceExtensionModule emitNotificationEvent method
[NotificationServiceExtensionModule.sharedInstance emitNotificationEvent:userInfo ];
completionHandler(UIBackgroundFetchResultNoData);
}
NotificationServiceExtensionModule.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
NotificationServiceExtensionModule.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
您可以使用以下代碼檢查應用程序是在后台還是前台運行:
- (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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.