I'am implementing a solution to allow the user to receive push notifications only from the list of users he is following. I read about advanced targeting recipients when pushing messages. How should I setup up my installation object in order to receive posts only from the users I’m following. Should I fetch the list of users he is following and setup the installation object. The issue is, I’m using Parse login view controller and I’ll get the list of users I’am following, only after I login. My second question is if the user will be able to get the push notifications if he is not logged in. My third question, is a new Parse installation object is created every time the app is launched? The requirement is I should see push notification from users I'am following. Please advise.
I have had similar questions and concerns while implementing my own authentication and push alert system. This is how I handled it.
in AppDelegate.m:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
if (authenticated) {
NSString *user = someUniqueStringForUser;
[currentInstallation addUniqueObject:user forKey:#"channels"];
}
[currentInstallation setDeviceTokenFromData:deviceToken];
[currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
NSLog(#"Successful Registration %s", __PRETTY_FUNCTION__);
} else {
NSLog(#"Error %#, %s", error, __PRETTY_FUNCTION__);
}
}];
}
In your login ViewController.m
- (void)authenticatedAndRegisterForPush {
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)];
NSString *appUser = uniqueString;
[currentInstallation addUniqueObject:appUser forKey:#"channels"];
[currentInstallation saveInBackground];
}
I'm not 100% sure you'll need to add channels again since the didRegister method handles the logic, but I'm doing it and it seems to be working correctly. Then in whatever view controller you have to select people to follow you just call the PFInstallation object, subscribe it to channels, and save.
When you want to send push notifications, you'll send it to all people who are subscribed to those channels. Parse will fail to send a push notification if there is no device token information registered to the installation.
I believe there are some minor changes to remote/push notifications in iOS 8, so if you're building against the iOS SDK there may be minimal changes to the appDelegate methods.
If you want to remove push notifications for a user you'll have to do the opposite of the registration process. Call the installation, remove all channels it'll listen to, and then save.
To my understanding, PFInstallation looks to see if there is an installation instance related to the appBundle. So if you were to log out and log back in, it recognizes the device and installation as the same. I've only had issues with logging out and push notifications only if I didn't clean out the channels correctly. There is also a pain with testing when deleting and re-installing on the same device creating multiple installations. I haven't found a great solution to this particular problem, but if you do please let me know.
Hope that helps.
Related
I have run into a problem on Firefox with web notifications when allowing notifications, but not remembering the choice. Please see image
The problem occurs whenever the user allows notifications, but doesn't let Firefox remember the decision. Whenever the user does persist the decision, the notification shows just fine.
// Let's check whether notification permissions have already been granted
if (Notification.permission === "granted") {
// If it's okay let's create a notification
createNotification();
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== "denied") {
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
createNotification();
}
});
}
Here is the code I'm running for this notification. When the user allows the notification, the code always gets to the second createNotification(), whether the choice was remembered or not. The permission is always equal to "granted". However, when creating the notification when the choice is remembered, the notification shows. When not remembering, the code executes just fine (including the creation of the notification) but no notification is displayed. Any guesses?
Turns out, this is a bug, see Bugzilla
I'm building an iOS game using Google Play Game for turn-based multiplayer but cannot receive any notifications. I did research many days to find what is wrong but i can't find anything.
So is iOS push notification in Google Play game still working at this time? Do your games still receive push notifications normally? Please provide me some information.
Thank you!
It is still indicated in the documentation that invitation and turn notifications are currently supported on Android and iOS. Based from this tutorial, you need to register your app to receive push notifications.
In your appDelegate, add the following code to the callback for retrieving the push device token passed back from APNS. This device token is used for outbound push notifications, and your app must register it with the Google Play games services push service. Make sure to always register your device token through GPGManager to enable push notifications from Google Play games services. Even if the user has not signed in; the GPGmanager object will cache this token and save it until the user signs in.
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken
:(NSData *)deviceToken {
NSLog(#"Got deviceToken from APNS! %#", deviceToken);
[[GPGManager sharedInstance] registerDeviceToken:deviceToken
forEnvironment:GPGPushNotificationEnvironmentSandbox];
}
From this related thread:
If you are having issues you might want to make sure the token is being registered correctly with APNS in the AppController.m (by logging it).
Also, there are two certificates you can register on the Play console, one for sandbox and one for production. Make sure that certificate you are using is the one being configured. By default the AppController calls
gpg::RegisterDeviceToken(deviceToken, false); which indicates that this is the prod certificate configured on the console.
You can also check this related GitHub issue which might help.
I try to register only alert type notification at application start in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
by calling
UIUserNotificationType types = UIUserNotificationTypeAlert;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
In
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
I permanently get all types in notificationSettings
<UIUserNotificationSettings: 0x16dd6160; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);>
And
UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings]
gives me the same all types despite my initial choice of the only alert type.
So I can't setup restricted dynamical permissions on start.
There is no any information about similar problems in the internet.
It seems that it is impossible to setup UserNotificationType from application in iOS8 (at least). One needs to use general Notification Center to set any combination of sound, badge and alert. Only the first attempt to register which occurs with Push notification permissions alert sets UserNotificationType needed.
Another way is to send UserNotificationType on subscription to your push server which should make payload depending of this type.
You must register notifications in (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
UPDATE:
According to the Apple documentation:
The first time you call the registerUserNotificationSettings: method,
iOS presents a dialog that asks the user for permission to present the
types of notifications the app registered. After the user replies, iOS
asynchronously calls back to the UIApplicationDelegate object with the
application:didRegisterUserNotificationSettings: method, passing a
UIUserNotificationType object that specifies the types of
notifications the user allows.
Users can change their notification settings at any time using the
Settings app. Your app is added to the Settings app as soon as you
call registerUserNotificationSettings:. Users can enable or disable
notifications, as well as modify where and how notifications are
presented. Because the user can change their initial setting at any
time, call currentUserNotificationSettings before you do any work
preparing a notification for presentation
So if user has already accepted Notification settings, application can't change them. currentUserNotificationSettings always show current user settings, not application settings.
We want to make a native application using Apple push notification. We want to make a native application using Apple push notification. The problem is we want to custom the system notification when users hover on it. We notice that Skype can show the Reply button when you hover on its notification .
Does anyone know how to create a notification like Skype does?
We'd also like to be able to make a couple of other modifications as well:
Show a different notification on hover.
Show a different notification on hover, preferably with a custom view, possibly including an image or webview?
Thanks.
Update:
I found that Skype don't use APNS to push new message (when Skype doesn't run, you won't see the notification when a new message arrive). The notification on the screen is a local notification. So, in my case which using remote notification, I ignore the alert key of remote notification payload and when the app receives a remote notification, it will remove this notification from Notification Center and push a new local notification. Here is the code:
func application(application: NSApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
println(userInfo)
// remove the remote one
let deliveredNotifications = NSUserNotificationCenter.defaultUserNotificationCenter().deliveredNotifications
if let lastRemoteNotif = deliveredNotifications.last as? NSUserNotification {
NSUserNotificationCenter.defaultUserNotificationCenter().removeDeliveredNotification(lastRemoteNotif)
}
// push another local notification
let localNotif = NSUserNotification()
localNotif.title = ""
localNotif.deliveryDate = NSDate()
localNotif.title = "CeillingNinja"
localNotif.subtitle = "This is local notification"
localNotif.informativeText = "Some text"
localNotif.contentImage = NSImage(named: "Status")
NSUserNotificationCenter.defaultUserNotificationCenter().deliverNotification(localNotif)
}
But when the app isn't running, user will see an empty notification in Notification Center. I still stuck at that point!
I am looking at ways on sending message from my main app to helper app. Its just a one way communication. I came accross XPC, unix sockets and NSDistributioncenter and found that posting notifications was the simplest one.
I am using postNotificationName:object: to send notifications.
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:#"aNote"
object:#"Hello"];
In helper app I have added observers in applicationDidFinishLaunching to receive notifications.
[[NSDistributedNotificationCenter defaultCenter] addObserver:self
selector:#selector(hello:)
name:#"aNote"
object:nil];
But it can only get notified if sandboxing is turned off. In the Apple docs it does say that userInfo must be nil if app is sandboxed but I am not using userInfo. I also tried with postNotificationName:object:userInfo: with userInfo nil but still it didnt work.
How can I get this to work in Sandboxed apps ?