How can I test Apple Push Notification Service without an iPhone?

喜夏-厌秋 提交于 2019-11-27 06:35:31
casademora

(This answer may be outdated after 2013)

Sorry to say, but you'll need to find some hardware to test this functionality.

Push notifications are not available in the simulator. They require a provisioning profile from iTunes Connect, and thus are required to be installed on a device. That also means you'll probably have to be accepted into the apple iPhone developer program and pay your $99.

On the bright side, with the iPhone OS 3.0 update, you can test this functionality on any device, including the first gen iPhones.

You can't test real push notifications. However, you can test your app's response to a simulated push notification by creating one programmatically and manually triggering your AppDelegate's - application:application didReceiveRemoteNotification:notification method.

To trigger this from a different class (like a UIViewController):

[[[UIApplication sharedApplication] delegate]
                    application:[UIApplication sharedApplication]
   didReceiveRemoteNotification:testNotification];

The testNotification should have the same format as a real notification, namely an NSDictionary that contains property list objects plus NSNull.

Here's an example of how to provide the testNotification above:

NSMutableDictionary *notification = [[NSMutableDictionary alloc] init];
[notification setValue:@"Test" forKey:@"alert"];
[notification setValue:@"default" forKey:@"sound"];

NSMutableDictionary *testNotification = [[NSMutableDictionary alloc] init];
[testNotification setValue:notification forKey:@"aps"];

This should create a reasonable notification NSDictionary to work with.

Nowadays, we can test push notifications with this library.

It's pretty easy to send push via terminal:

echo -n '{"message":"message"}' | nc -4u -w1 localhost 9930

echo -n '{"aps":{"alert" : "message","badge" : 99,"sound" : "default"}, "myField" : 54758}' | nc -4u -w1 localhost 9930

The simulator does not do Push Notifications.

And to push from a server, you have to have device(s) to push to as well as your app on that device.

The token contains the app identity as well as the device ID.

you have to use

NSString *notificationString = @"{\"aps\":{\"alert\":\"Test alert\",\"sound\":\"default\"}}";

NSData *notificationData = [notificationString dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *testNotification = [NSJSONSerialization JSONObjectWithData:notificationData options:0 error:&error];

[[[UIApplication sharedApplication] delegate] application:[UIApplication sharedApplication] didReceiveRemoteNotification:testNotification  fetchCompletionHandler:nil];

Yes you can check push notification on simulator but you have to use a library in your app Named SimulatorRemoteNotifications. By which by using just 4-5 steps you can test push notification on Simulator.

They Also Provide PODs too

pod 'SimulatorRemoteNotifications', '~> 0.0.3'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!