我是Objective-C语言的新手。我需要实现的只是我的应用程序上的推送通知位。我使用javapns库在XCode 6和Java服务器端编写了客户端。现在,当服务器设法发送通知(我收到确认消息)时,我的设备上没有收到任何内容,无论是应用程序处于活动状态还是在后台运行。
有人可以指导我朝正确的方向前进吗?谢谢!
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert |UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken{
//Store the Device Token
NSLog(@"%@", newDeviceToken);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSLog(@"Failed to register with error: %@", error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"Push received: %@", userInfo);
}
服务器端:
public class PushServer {
public static void main(String[] args) {
try {
BasicConfigurator.configure();
Push.alert("Message!", "***.p12", "***", false,
"92ab*************91af4");
} catch (CommunicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeystoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是我尝试发送通知时收到的输出:
0 [main] DEBUG javapns.notification.Payload - Adding alert [Message!]
210 [main] DEBUG javapns.communication.ConnectionToAppleServer - Creating SSLSocketFactory
229 [main] DEBUG javapns.communication.ConnectionToAppleServer - Creating SSLSocket to gateway.sandbox.push.apple.com:2195
1077 [main] DEBUG javapns.notification.PushNotificationManager - Initialized Connection to Host: [gateway.sandbox.push.apple.com] Port: [2195]: 735b478[SSL_NULL_WITH_NULL_NULL: Socket[addr=gateway.sandbox.push.apple.com/17.172.232.46,port=2195,localport=53762]]
1079 [main] DEBUG javapns.notification.PushNotificationManager - Building Raw message from deviceToken and payload
1080 [main] DEBUG javapns.notification.PushNotificationManager - Built raw message ID 1 of total length 73
1080 [main] DEBUG javapns.notification.PushNotificationManager - Attempting to send notification: {"aps":{"alert":"Message!"}}
1080 [main] DEBUG javapns.notification.PushNotificationManager - to device: 92a**********1af4
2327 [main] DEBUG javapns.notification.PushNotificationManager - Flushing
2327 [main] DEBUG javapns.notification.PushNotificationManager - At this point, the entire 73-bytes message has been streamed out successfully through the SSL connection
2327 [main] DEBUG javapns.notification.PushNotificationManager - Notification sent on first attempt
2327 [main] DEBUG javapns.notification.PushNotificationManager - Reading responses
2749 [main] DEBUG javapns.notification.PushNotificationManager - Found 0 notifications that must be re-sent
2749 [main] DEBUG javapns.notification.PushNotificationManager - No notifications remaining to be resent
2749 [main] DEBUG javapns.notification.PushNotificationManager - Closing connection
感谢任何帮助。
答案 0 :(得分:1)
您偶然会使用Xcode 7构建并定位到iOS 9吗?
如果是这样,您可能会遇到新的安全默认设置,需要安全地访问网址,如下所述:
Disabling ATS for an in-app browser in iOS 9?
此外,您似乎将旧方法与新方法混合在一起。
registerUserNotificationSettings是一个取代了registerForRemoteNotificationTypes的iOS 8方法。
所以,只使用前者,看起来你只针对iOS 8及更高版本。
但是你使用didReceiveRemoteNotification:这是该方法的旧版本,对应于registerForRemoteNotificationTypes。但是,由于您使用的是较新的版本,因此您应该使用它 didReceiveRemoteNotification:fetchCompletionHandler
答案 1 :(得分:-1)
您需要将NSData
转换为字符串,并通过在您的方法中实施以下更改来获取实际的设备令牌。
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *myToken = [[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]
stringByReplacingOccurrencesOfString: @">" withString: @""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
// *** Now user `myToken` to send notification ***
NSLog(@"%@",myToken);
}
除此之外,您还需要确保运行的应用程序具有相同的临时配置文件(开发人员/分发),用于在服务器端发送推送通知。两者都应该是一样的。我希望你能清楚。