I am trying to register an app for intercoms push notifications. They have instructions here: https://docs.intercom.io/Install-on-your-mobile-product/enabling-push-notifications-with-intercom-for-ios
They give this code in obj c, but my app is in swift and this looks like gibberish to me:
- (void)applicationDidBecomeActive:(UIApplication *)application {
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]){ // iOS 8 (User notifications)
[application registerUserNotificationSettings:
[UIUserNotificationSettings settingsForTypes:
(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil]];
[application registerForRemoteNotifications];
} else { // iOS 7 (Remote notifications)
[application registerForRemoteNotificationTypes:
(UIRemoteNotificationType)
(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)];
}
}
Could someone explain how to do this process in swift?
Following code works on Xcode 7.1 by using the new availability feature added to Swift 2
if #available(iOS 8, *) {
application.registerUserNotificationSettings(
UIUserNotificationSettings(forTypes: UIUserNotificationType(rawValue: UIUserNotificationType.Badge.rawValue |
UIUserNotificationType.Sound.rawValue |
UIUserNotificationType.Alert.rawValue),
categories: nil))
} else {
application.registerForRemoteNotificationTypes(
UIRemoteNotificationType(rawValue: UIRemoteNotificationType.Badge.rawValue |
UIRemoteNotificationType.Alert.rawValue |
UIRemoteNotificationType.Sound.rawValue))
}
Related
I want to use remote notification with deployment target 8.1 and xcode 6.1
i am using following code
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
But it shows me deprecated methods in else part....so now how can i get rid of it...
and the other code what i tried is:
You could ignore deprecate warning like this:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
#pragma clang diagnostic pop
}
#else
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
#endif
See this post:
Hope the following code might serve your need
if ([application respondsToSelector:#selector(isRegisteredForRemoteNotifications)])
{
// iOS 8 Notifications
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeNewsstandContentAvailability |
UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
The method registerForRemoteNotificationTypes: is depricated from iOS 8.0. Since you are compiling using iOS 8.1 SDK you will get the warning. It is an expected behavior and your application will work properly as your application will use registerForRemoteNotificationTypes: if and only if your iOS version is less than 8.0 where the method registerUserNotificationSettings: is defined.
If you need to avoid any depreciation warning you can use -Wno-deprecated-declarations flag to suppress the warning.
ok. I can understand your problem you have used deployment target 8.1.
just change deployment target less than or equal to 7, otherwise remove else part because no need to define this in 8.1 target
I followed this tutorial and set everything just like it says to receive push notifications on iOS.
I set the didFinishLaunchingWithOptions in Xcode with this:
if ([application respondsToSelector:#selector(isRegisteredForRemoteNotifications)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil]];
[application registerForRemoteNotifications];
} else {
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
I got registered(I am able to get the deviceToken) and i can get the message from the push notification when is sent, the problem is that i can't get to play any notification sound, my pusher.php is like this:
$body['aps'] = array(
'alert' => $message,
'sound' => "default"
);
I am using iOS 8.1.3
I got the same problems, Seems like the Code below is obsolete:
if ([application respondsToSelector:#selector(isRegisteredForRemoteNotifications)])
{
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound |
UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
}
else
{
[application registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
if (application) {
application.applicationIconBadgeNumber = 0;
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|
UIUserNotificationTypeBadge|
UIUserNotificationTypeSound categories:nil]];
}
}
return YES;}
As I'am only getting Local notifications within the App. Even after putting correct certificate to this.
So far Iv not found any help with this, so ended up emailing Apple Developer Technical Support about this.
NOTE: This was apparently due to some sort of corruption in the Settings, the code was apparently fine all along...
Following the conversation in this thread: Remote Notification iOS 8, I added this boilerplate code to my app:
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIRemoteNotificationTypeBadge categories:nil]];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert];
}
if ([[inLaunchOptions allKeys] containsObject:UIApplicationLaunchOptionsRemoteNotificationKey]) {
[appTracking registerPush:[inLaunchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]];
}
I also added the application:didRegisterUserNotificationSettings handler, which calls a single line of code, [application registerForRemoteNotifications]. This code is called when the app starts.
Reading that thread, it is suggested that this is all that is required, and that this will cause application:didRegisterForRemoteNotificationsWithDeviceToken to be called. But that is definitely not happening in my app.
Did I miss a step, or simply mis-understand how to set this up?
Try This :
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
} else {
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound)];
}
you need to add the following line [application registerForRemoteNotifications] after [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert]; iOS 8 condition
Good Luck
Me and my team suffering from one month because of this issue, problem is apple push notifications are working for some time in all installed devices, but after that even one device also not getting any notifications, this is happening continuously please solve this problem. Where is that problem and how to resolve this issue, please help me. I've written the below code in didFinishLaunchingWithOptions method of AppDelegate
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
if you want to register for iOS 8 and iOS 7 need to do so in didFinishLaunchingWithOptions:
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)])
{
// iOS 8 Notifications
// use registerUserNotificationSettings
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
// iOS < 8 Notifications
// use registerForRemoteNotifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
}
Do you know a way to pass from UIUserNotificationTypeNone to UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound (iOS8)?
To pass to UIUserNotificationTypeNone works well but not when the user try to switch again to enable ...
This is my code:
// Register for Push Notifications, if running iOS 8
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)])
{
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
UIUserNotificationSettings * settingsAvailable = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
[application registerUserNotificationSettings:settingsAvailable];
[application registerForRemoteNotifications];
}
else
{
// Register for Push Notifications before iOS 8
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
After switch to enable the method of delegate "didRegisterUserNotificationSettings"
// Register to receive notifications
[application registerForRemoteNotifications];
NSLog(#"The app is registered for the types: %#", notificationSettings);
returns:
The app is registered for the types: <UIUserNotificationSettings: 0x15e9e790; types: (none);>
Thanks in advance
That's because the notification setting has been turned off in the system setting.