我正在使用FCM实现iOS应用的推送通知。我的Pushnotification API是用C#.net。
制作的我的问题是,当我的应用程序处于开发模式时,通知会起作用,但在为客户端创建二进制文件(来自:diawi.com)之后,即使我从服务器获得成功消息,通知也不再起作用。
我遵循StackOverflow的许多链接,但没有成功。
API代码:
var applicationID ="AAAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxCUxxxxeA";
var senderId = "2771xxxxxx16";
deviceIdAsus = "cEOfJfqoxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxMpGc";
body = "Pushnotification Iphone TestMessage";
title = "Test";
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var data = new
{
to = deviceIdAsus,
notification = new
{
body = body,
title = title,
icon = "myicon",
sound = "default",
},
data = new
{
body = body,
title = title,
icon = "myicon",
sound = "default",
},
priority = "high"
};
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(data);
Byte[] byteArray = Encoding.UTF8.GetBytes(json);
tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
tRequest.ContentLength = byteArray.Length;
using (Stream dataStream = tRequest.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
using (WebResponse tResponse = tRequest.GetResponse())
{
using (Stream dataStreamResponse = tResponse.GetResponseStream())
{
using (StreamReader tReader = new StreamReader(dataStreamResponse))
{
String sResponseFromServer = tReader.ReadToEnd();
response = sResponseFromServer;
}
}
}
}
回应:
{
"Status": "True",
"Response": "{\"multicast_id\":5525338412933884291,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1482845931819882%b367a02ab367a02a\"}]}"
}
IOS代码(目标c):
#define Application_Type @"Production"
Appdelegate.m
didFinishLaunchingWithOptions{
if (floor(NSFoundationVersionNumber) < NSFoundationVersionNumber10_0) {if (floor(NSFoundationVersionNumber) < NSFoundationVersionNumber10_0) {
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
UNAuthorizationOptions authOptions =
UNAuthorizationOptionAlert
| UNAuthorizationOptionSound
| UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]10_
requestAuthorizationWithOptions:authOptions
];
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
[FIRMessaging messaging].remoteMessageDelegate = self;
#endif
}
[[UIApplication sharedApplication] registerForRemoteNotifications]; UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE_10_0
UNAuthorizationOptions authOptions =
UNAuthorizationOptionAlert
| UNAuthorizationOptionSound
| UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions
];
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
[FIRMessaging messaging].remoteMessageDelegate = self;
#endif
}
[[UIApplication sharedApplication] registerForRemoteNotifications];
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
[UIApplication.sharedApplication registerForRemoteNotificationTypes: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound];
[application registerForRemoteNotifications];
}
[FIRApp configure];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(onTokenRefresh)
name:kFIRInstanceIDTokenRefreshNotification object:nil];
}
- (void)applicationWillTerminate:(UIApplication *)application {
[[FIRMessaging messaging] disconnect];
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)settings{
NSLog(@"Registering device for push notifications..."); // iOS 8
if (floor(NSFoundationVersionNumber) < NSFoundationVersionNumber10_0) {
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
UNAuthorizationOptions authOptions =
UNAuthorizationOptionAlert
| UNAuthorizationOptionSound
| UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions
];
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
[FIRMessaging messaging].remoteMessageDelegate = self;
#endif
}
[[UIApplication sharedApplication] registerForRemoteNotifications];
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSLog(@"deviceToken %@",deviceToken);
NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
_strDeviceToke = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
if (_strDeviceToke) {
NSLog(@"Device %@",_strDeviceToke);
#if DEBUG
[[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];
#else
[[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeProd];
#endif
NSString *refreshedToken = [[FIRInstanceID instanceID] token];
NSLog(@"FireBase InstanceID token: %@", refreshedToken);
if (refreshedToken.length ==0) {
_strFireBaseToken =refreshedToken;
[[NSUserDefaults standardUserDefaults]setValue:refreshedToken forKey:@"NotificationToken"];
}
[self connectToFcm];
}
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
if (application.applicationState == UIApplicationStateActive) {
[application setApplicationIconBadgeNumber:0];
}else if (application.applicationState==UIApplicationStateBackground){
}
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
}
- (void)connectToFcm {
[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
if (error != nil) {
} else {
}
}];
}
并更改代码签名 - &gt;临时个人资料 - &gt;发布 - &gt;分发证书
有人能指出我正确的方向吗?