如何在 voip 中接收消息?我可以得到VoIP令牌!
我在目标C中写道:
我已经在互联网上搜索过,但找不到任何解决方案!我找到的只是 Swift,但无法在 Swift 中获取令牌。 在目标 C 中,我可以获取令牌,但不能从一行中获取消息。
apn push "<Token>" -c backgroundfetch.pem -m "Hello"
如何在目标 C 中弹出消息?这是我的代码:
#import "AppDelegate.h"
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@import PushKit;
@import UserNotifications;
@interface AppDelegate ()
@end
@implementation AppDelegate
NSString *fcmToken = @"";
// Trigger VoIP registration on launch
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self voipRegistration];
return YES;
}
// Register for VoIP notifications
- (void) voipRegistration {
dispatch_queue_t mainQueue = dispatch_get_main_queue();
// Create a push registry object
PKPushRegistry * voipRegistry = [[PKPushRegistry alloc] initWithQueue: mainQueue];
// Set the registry's delegate to self
voipRegistry.delegate = self;
// Set the push type to VoIP
voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
}
- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type{
if([credentials.token length] == 0) {
NSLog(@"voip token NULL");
return;
}
NSLog(@"PushCredentials: %@", credentials.token);
}
// Handle incoming pushes
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type {
// Process the received push
NSLog(@"Get");
}
@end
我的info.plist
:
<key>UIBackgroundModes</key>
<array>
<string>voip</string>
<string>audio</string>
<string>fetch</string>
<string>remote-notification</string>
</array>
在您的情况下,解决方案是发送静默推送通知。静默推送通知到达后,使用后台任务执行代码。
以下是发送静默推送通知的良好解释。
https://stackoverflow.com/a/36327058/9106403