I am trying to set up push notifications with Parse Push and Xamarin IOS. I have followed these guides:
https://www.parse.com/docs/dotnet/guide#push-notifications-push-on-xamarin-ios https://www.parse.com/apps/quickstart#parse_push/ios/xamarin/existing https://developer.xamarin.com/guides/cross-platform/application_fundamentals/notifications/ios/remote_notifications_in_ios/ https://groups.google.com/forum/#!topic/parse-developers/65WAfRIiEnA
From my understanding of those I modified my AppDelegate as such,
I added this to the constructor:
ParseClient.Initialize("MY APP ID", "MY DOT NET KEY");
With the appropriate keys.
And I added this to the FinishedLaunching method
if (Convert.ToInt16(UIDevice.CurrentDevice.SystemVersion.Split('.')[0].ToString()) < 8) {
UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
} else {
UIUserNotificationType notificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
var settings = UIUserNotificationSettings.GetSettingsForTypes(notificationTypes, new NSSet(new string[] { }));
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
UIApplication.SharedApplication.RegisterForRemoteNotifications();
}
// Handle Push Notifications
ParsePush.ParsePushNotificationReceived += (object sender, ParsePushNotificationEventArgs args) => {
Utils.Log("Push!");
};
Then I added the appropriate overrides:
public override void DidRegisterUserNotificationSettings(UIApplication application,
UIUserNotificationSettings notificationSettings) {
application.RegisterForRemoteNotifications();
}
public override void RegisteredForRemoteNotifications(UIApplication application,
NSData deviceToken) {
ParseInstallation installation = ParseInstallation.CurrentInstallation;
installation.SetDeviceTokenFromData(deviceToken);
installation.SaveAsync();
}
public override void FailedToRegisterForRemoteNotifications (UIApplication application , NSError error)
{
new UIAlertView("Error registering push notifications", error.LocalizedDescription, null, "OK", null).Show();
}
public override void ReceivedRemoteNotification(UIApplication application,
NSDictionary userInfo) {
// We need this to fire userInfo into ParsePushNotificationReceived.
ParsePush.HandlePush(userInfo);
}
But still no luck.
I added a breakpoint in the RegisteredForRemoteNotifications method and it does get called so I am apparently registering for notifications but when I try and send a push notification from parse it tells me "No Devices Registered".
Things I have tried:
- Checking provisioning profiles are set up for push notifications.
- Deleting all provisioning profiles remote and local and then regenerating them.
- Making sure by build is using the new provisioning profiles.
- Regenerating the .p12 file and re-uploading it to parse.
- Checking my bundle identifier is consistent in Info.plist and prov profiles.
But I would imagine it is not an issue with the provisioning profiles because the App is registering for notifications,
Why does Parse Disagree?
What am I missing?
I'm going through the same issue as well. I've followed the tutorial and recreated provisioning profiles, .p12 etc just as you have. Still no luck. I went the manual way following the example here - https://www.parse.com/docs/rest/guide/#push-notifications-uploading-installation-data and registered my device via a curl command:
curl -X POST \
-H "X-Parse-Application-Id: <Your Parse app ID> " \
-H "X-Parse-REST-API-Key: <Your parse REST API key>" \
-H "Content-Type: application/json" \
-d '{
"deviceType": "ios",
"deviceToken": "<Your device token>",
"channels": [
""
]
}' \
https://api.parse.com/1/installations
After doing that I immediately saw my device as registered. I tried to send a push notification but have not received it yet. Something tells me that there's an issue with the Xamarin installation or with certificates/provisioning profiles. I'm still working on this, if I resolve it I will update.
Edit 1: I found out that if you uninstall the app you're working on and re-install then your device token changes. Anyway so I tried something different again, I fired up my app and ran it in debug mode via xamarin. I grabbed the new device token and issued that curl command again. Parse now tells me I have two devices registered. I hit the home button on my app and told parse to fire off a push notification and I received it. So now I'm investigating if the app is properly registering with parse. It seems like it tries to communicate but there might be an error somewhere and I'm not sure what it might be.
Update: So I finally got my device to register but going a totally different way. Here's what my RegisteredForRemoteNotifications looks like now. (Note: I am using Parse 1.5.5 as newer versions did not work at the time of this writing. Currently the newest version is 1.6.2.)
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) {
ParseObject obj = ParseObject.Create ("_Installation");
string dt = deviceToken.ToString ().Replace ("<", "").Replace (">", "").Replace (" ", "");
obj["deviceToken"] = dt;
obj.SaveAsync ().ContinueWith (t => {
if (t.IsFaulted) {
using (IEnumerator<System.Exception> enumerator = t.Exception.InnerExceptions.GetEnumerator()) {
if (enumerator.MoveNext()) {
ParseException error = (ParseException) enumerator.Current;
Console.WriteLine ("ERROR!!!: " + error.Message);
}
}
} else {
Console.WriteLine("Saved/Retrieved Installation");
var data = NSUserDefaults.StandardUserDefaults;
data.SetString ("currentInstallation", obj.ObjectId);
Console.WriteLine("Installation ID = " + obj.ObjectId);
}
});
}
I'd still like to know why we can't do
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) {
ParseInstallation installation = ParseInstallation.CurrentInstallation;
installation.SetDeviceTokenFromData(deviceToken);
installation.SaveAsync();
}
But for now I have something that works.
来源:https://stackoverflow.com/questions/33041335/no-devices-registered-with-parse-push-and-xamarin-ios