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 ?
Related
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.
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.
Does anyone know of a way to subscribe to events in the Spotify application? To subscribe to iTunes events, you would just add an observer to the notification center like this:
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:#selector(changedTrack:) name:#"com.apple.iTunes.playerInfo" object:nil];
Thereby all events will be send in an NSNotification to changedTrack:.
I can't seem to find a similar (or any way) to do this for Spotify but I know there are applications doing this, for example applications showing the song currently being played.
Spotify's NSDistributedNotification name is com.spotify.client.PlaybackStateChanged.
Subscribe to that using NSDistributedNotificationCenter and you'll get a notification very similar to iTunes'.
I created a firebreath plugin that sends a notification to a cocoa application. Is there any way to get a response from the application if the notification was recived successfully?
Assuming you can receive them in firebreath plugins, why don't you send an NSDistributedNotification back? I've used this mechanism to communicate an app with a background agent and it works perfectly.
[[NSDistributedNotificationCenter defaultCenter]
postNotificationName:<#ping-or-pong#>
object:<#sender-id#>
userInfo:nil
deliverImmediately:NO];
You can use <#sender-id#> (a string, for instance) to allow each part to skip its own notifications.