Xamarin.iOS 应用在激活时未收到 parse.com 推送
Xamarin.iOS app does not receive parse.com PUSH when active
我可以从我的网站通过 parse.com 和他们的 API 向我的用户发送推送。
但是只有当应用程序处于非活动状态时推送才会到达,例如在后台或 turned/killed 关闭。
如果该应用是活动应用,则不会显示推送。
我希望推送像在应用程序外一样到达(例如出现在顶部并出现在 iOS 的通知日志中的通知)
我的代码是在 Xamarin.iOS 中编写的,几乎使用默认的 parse.com Xamarin.iOS 预制模板。
我应该更改什么以使其接收推送并像正常一样对待它们,即使在应用程序处于活动状态时也是如此?
此应用已发布,可以实时运行和调试。但是我的问题是激活时推送没有到达。
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
// Initialize the Parse client with your Application ID and .NET Key found on
// your Parse dashboard
ParseClient.Initialize("key1", "key2");
// Register for Push Notitications
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) => {
// Process Push Notification payload here.
};
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
public override void DidRegisterUserNotificationSettings(UIApplication application, UIUserNotificationSettings notificationSettings)
{
application.RegisterForRemoteNotifications();
}
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;
}
}
}
else
{
var data = NSUserDefaults.StandardUserDefaults;
data.SetString("currentInstallation", obj.ObjectId);
}
});
parseDeviceInfo.deviceToken = dt;
//Original parse.com code that does not work
//ParseInstallation installation = ParseInstallation.CurrentInstallation;
//installation.SetDeviceTokenFromData(deviceToken);
//installation.SaveAsync();
}
public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
// We need this to fire userInfo into ParsePushNotificationReceived.
ParsePush.HandlePush(userInfo);
}
感谢您的宝贵时间。
这是解决方案。
里面ParsePush.ParsePushNotificationReceived上面可以看到
// Process Push Notification payload here.
string message = "";
try
{
var payload = args.Payload;
object aps;
if (payload.TryGetValue("aps", out aps))
{
string payloadStr = "";
try
{
payloadStr = aps.ToString();
}
catch (Exception e)
{
}
try
{
var match = Regex.Match(payloadStr, @"alert = (.*);\n");
if (match.Success)
{
string alertText = match.Groups[1].Value;
message = alertText;
}
}
catch (Exception)
{
}
}
}
catch (Exception e)
{
message = "payload crash";
}
if (string.IsNullOrEmpty(message))
message = "";
UILocalNotification notification = new UILocalNotification();
//NSDate.FromTimeIntervalSinceNow(15);
notification.FireDate = NSDate.FromTimeIntervalSinceNow(5);
//notification.AlertTitle = "Alert Title"; // required for Apple Watch notifications
notification.AlertAction = "Message";
notification.AlertBody = message;
notification.SoundName = UILocalNotification.DefaultSoundName;
UIApplication.SharedApplication.ScheduleLocalNotification(notification);
还需要增加这个功能
这也在 AppDelegate
public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
{
try
{
// show an alert
UIAlertController okayAlertController = UIAlertController.Create(notification.AlertAction, notification.AlertBody, UIAlertControllerStyle.Alert);
okayAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
//viewController.PresentViewController(okayAlertController, true, null);
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(okayAlertController, true, null);
// reset our badge
UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
}
catch (Exception)
{
}
}
我可以从我的网站通过 parse.com 和他们的 API 向我的用户发送推送。
但是只有当应用程序处于非活动状态时推送才会到达,例如在后台或 turned/killed 关闭。 如果该应用是活动应用,则不会显示推送。
我希望推送像在应用程序外一样到达(例如出现在顶部并出现在 iOS 的通知日志中的通知)
我的代码是在 Xamarin.iOS 中编写的,几乎使用默认的 parse.com Xamarin.iOS 预制模板。
我应该更改什么以使其接收推送并像正常一样对待它们,即使在应用程序处于活动状态时也是如此?
此应用已发布,可以实时运行和调试。但是我的问题是激活时推送没有到达。
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
// Initialize the Parse client with your Application ID and .NET Key found on
// your Parse dashboard
ParseClient.Initialize("key1", "key2");
// Register for Push Notitications
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) => {
// Process Push Notification payload here.
};
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
public override void DidRegisterUserNotificationSettings(UIApplication application, UIUserNotificationSettings notificationSettings)
{
application.RegisterForRemoteNotifications();
}
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;
}
}
}
else
{
var data = NSUserDefaults.StandardUserDefaults;
data.SetString("currentInstallation", obj.ObjectId);
}
});
parseDeviceInfo.deviceToken = dt;
//Original parse.com code that does not work
//ParseInstallation installation = ParseInstallation.CurrentInstallation;
//installation.SetDeviceTokenFromData(deviceToken);
//installation.SaveAsync();
}
public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
// We need this to fire userInfo into ParsePushNotificationReceived.
ParsePush.HandlePush(userInfo);
}
感谢您的宝贵时间。
这是解决方案。
里面ParsePush.ParsePushNotificationReceived上面可以看到
// Process Push Notification payload here.
string message = "";
try
{
var payload = args.Payload;
object aps;
if (payload.TryGetValue("aps", out aps))
{
string payloadStr = "";
try
{
payloadStr = aps.ToString();
}
catch (Exception e)
{
}
try
{
var match = Regex.Match(payloadStr, @"alert = (.*);\n");
if (match.Success)
{
string alertText = match.Groups[1].Value;
message = alertText;
}
}
catch (Exception)
{
}
}
}
catch (Exception e)
{
message = "payload crash";
}
if (string.IsNullOrEmpty(message))
message = "";
UILocalNotification notification = new UILocalNotification();
//NSDate.FromTimeIntervalSinceNow(15);
notification.FireDate = NSDate.FromTimeIntervalSinceNow(5);
//notification.AlertTitle = "Alert Title"; // required for Apple Watch notifications
notification.AlertAction = "Message";
notification.AlertBody = message;
notification.SoundName = UILocalNotification.DefaultSoundName;
UIApplication.SharedApplication.ScheduleLocalNotification(notification);
还需要增加这个功能 这也在 AppDelegate
public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
{
try
{
// show an alert
UIAlertController okayAlertController = UIAlertController.Create(notification.AlertAction, notification.AlertBody, UIAlertControllerStyle.Alert);
okayAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
//viewController.PresentViewController(okayAlertController, true, null);
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(okayAlertController, true, null);
// reset our badge
UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
}
catch (Exception)
{
}
}