我可以从parse.com发送推送通知,它可以很好地到达设备,但是当从设备发送到设备时它不会发送。然后就像你在图片中看到的那样“推送发送 - 0”。
以下是AppDelegate.swift文件中的代码:
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// Override point for customization after application launch.
FBLoginView.self
FBProfilePictureView.self
initLocationManager();
Parse.setApplicationId("key", clientKey: "key")
let notificationTypes:UIUserNotificationType = UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert
let notificationSettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
UIApplication.sharedApplication().registerForRemoteNotifications()
return true
}
func application(application: UIApplication!, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings!) {
UIApplication.sharedApplication().registerForRemoteNotifications()
}
func application(application: UIApplication!, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData!) {
let currentInstallation:PFInstallation = PFInstallation.currentInstallation()
currentInstallation.setDeviceTokenFromData(deviceToken)
currentInstallation.save()
}
func application(application: UIApplication!, didFailToRegisterForRemoteNotificationsWithError error: NSError!) {
println(error.localizedDescription)
}
func application(application: UIApplication!, didReceiveRemoteNotification userInfo:NSDictionary!) {
var notification:NSDictionary = userInfo.objectForKey("aps") as NSDictionary
println(userInfo)
if(application.applicationState == UIApplicationState.Active) {
var ln: UILocalNotification = UILocalNotification()
ln.userInfo = userInfo
ln.soundName = UILocalNotificationDefaultSoundName
ln.alertBody = notification["alert"] as NSString
ln.fireDate = NSDate()
application.scheduleLocalNotification(ln)
println("local")
} else {
PFPush.handlePush(userInfo)
}
}
在视图控制器的发送消息方法中:
var pushQuery: PFQuery = PFInstallation.query()
pushQuery.whereKey("user", equalTo: SelectedUser.sharedInstance.getUsername())
var push:PFPush = PFPush()
push.setQuery(pushQuery)
var data: AnyObject = ["from" : GlobalVars.sharedInstance.getUserID(), "message" : txtMessage.text, "aps" : ["alert" : txtMessage.text, "badge" : "1"]] as NSDictionary
push.setData(data as NSDictionary)
push.sendPush(NSErrorPointer())
我觉得它与push.sendPush(NSErrorPointer())
行有关,我曾经在我的旧应用中使用push.sendInBackground()
,但这不再是一个选项?:
请帮助,我正在撕裂我的头发。
答案 0 :(得分:0)
我必须说我不太了解,但我认为你应该使用sendPushInBackgroundWithBlock()
。据我所知,没有后台块的解析方法将在主线程上执行,这不是一个好方法,因为它也会减慢主线程上运行的所有可视内容。
我使用带有目标c的解析但是我唯一一次使用withoud后台线程的方法,我在我创建的自己的后台块中使用它。
如果它不是问题,请告诉我为什么在后台发送推送不是一个选项。两种方法都需要完全相同的时间来执行,并且块只需要小心,以便主要的thred继续运行。我可以粘贴一些客观的c代码来向你展示我如何将解析方法包装在我自己的中。希望它会帮助你。
在头文件中,我创建了我的方法来从解析中获取所有联系人:
- (void)allContacts:(void(^)(NSArray *contacts))completion;
在实施文件中:
- (void)allContacts:(void (^)(NSArray *))completion
{
PFQuery *contactsQuery = [Contact query];
[contactsQuery whereKey:@"createdBy" equalTo:[PFUser currentUser]];
[contactsQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error && [objects count])
if (completion) completion(objects);
else
if (completion) completion(nil);
}];
}
所以我只是没有看到使用积木的任何缺点,在拳头看起来有点太复杂,但最后他们会帮助你很多。