Stop only firebase push-notification vibration - ios

How to stop firebase push notification vibration while app in fore ground in objective c?

You can try
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
if([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive ) {
completionHandler(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
else {
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}
}

Related

How to disable default notification alert view on iOS remote notifications?

In my app I enabled remote notifications,
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"10.0")){
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
if( !error ){
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}];
}else{
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
}
And I implemented the delegate methods as follows,
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSLog(#"Failed!!");
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Print message.
if (userInfo) {
NSLog(#"Message ID: %#", userInfo);
}
completionHandler(UIBackgroundFetchResultNoData);
}
But when the app is in foreground and receiving a notification, I got a UIAlertView with the notification title and message. I want to
//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
NSLog(#"User Info : %#",notification.request.content.userInfo);
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionBadge);
}
//Called to let your app know which action was selected by the user for a given notification.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSLog(#"User Info : %#",response.notification.request.content.userInfo);
completionHandler();
}
I debugged on willPresentNotification: and after this triggers I got the alert. And I tested by removing this method as well. But still this alert is showing when receiving a notification.
How may I disable this alert view? I'm using 10.2 iOS in my iPhone
I don't think the UIAlertController comes from notifications. Are you sure you are not creating one elsewhere ?
Your notifications' implementation seems ok.
Can you search in your project and using breakpoints to see if any of your UIAlertControllers get hit?
Use UNNotificationPresentationOptionNone in the completionHandler
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
NSLog(#"User Info : %#",notification.request.content.userInfo);
completionHandler(UNNotificationPresentationOptionNone);
}

Unable to access push notification when app is in background for iOS 10

For iOS 8 and 9, when the application is in the background, when a push notification is received, I am able to store the information it in the app. But for iOS 10, I cannot retrieve the information when the app is in the background. But only if I open/click on the notification, userNotification centerDidReceiveNotificationResponse is invoked.
For iOS 8 & 9, this works fine.
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// iOS 10 will handle notifications through other methods
if( SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO( #"10.0" ) )
{
NSLog( #"iOS version >= 10. Let NotificationCenter handle this one." );
// set a member variable to tell the new delegate that this is background
return;
}
NSLog( #"HANDLE PUSH, didReceiveRemoteNotification: %#", userInfo );
// custom code to handle notification content
if( [UIApplication sharedApplication].applicationState == UIApplicationStateInactive )
{
NSLog( #"INACTIVE" );
completionHandler( UIBackgroundFetchResultNewData );
}
else if( [UIApplication sharedApplication].applicationState == UIApplicationStateBackground )
{
NSLog( #"BACKGROUND" );
completionHandler( UIBackgroundFetchResultNewData );
}
else
{
NSLog( #"FOREGROUND" );
completionHandler( UIBackgroundFetchResultNewData );
}
}
For iOS 10 the method is not invoked,
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
NSLog( #"Handle push from foreground" );
// custom code to handle push while app is in the foreground
NSLog(#"%#", notification.request.content.userInfo);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)())completionHandler
{
NSLog( #"Handle push from background or closed" );
// if you set a member variable in didReceiveRemoteNotification, you will know if this is from closed or background
NSLog(#"%#", response.notification.request.content.userInfo);
}
I hope you have follow Below Steps.
Step 1 : Import "UserNotifications" framework.
#import <UserNotifications/UserNotifications.h>
Step 2 : And your AppDelegate confirming "UNUserNotificationCenterDelegate" delegate
#interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
Step 3 : For iOS10, You can ask for the permission like this
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
if(!error){
// Permission for notifications is granted. Do the other code
}
}];
Now I think the problem you are having in your code in Handling
delegate methods for UserNotifications.
//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
NSLog(#"User Info : %#",notification.request.content.userInfo);
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}
//Called to let your app know which action was selected by the user for a given notification.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSLog(#"User Info : %#",response.notification.request.content.userInfo);
completionHandler();
}
Your code is missing below line of codes
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
And
completionHandler();
In willPresentNotification and didReceiveNotificationResponse respectively.

Unable to listen notification from NSNotificationCenter when App received push notification from background or terminated in iOS

I'm implementing push notifications in iOS 10. Everything working well.
But I need to hit API when APP received push notification (not only in Active state but also in background/terminated).
For this I'm using NSNotificationCenter for listening notification when App received push notification like this :
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
NSDictionary *userInfo = notification.request.content.userInfo;
NSLog(#"Message ID: %#", userInfo[#"gcm.message_id"]);
NSLog(#"%#", userInfo);
if( [UIApplication sharedApplication].applicationState == UIApplicationStateInactive )
{
NSLog( #"INACTIVE" );
completionHandler( UNNotificationPresentationOptionAlert );
}
else if( [UIApplication sharedApplication].applicationState == UIApplicationStateBackground )
{
NSLog( #"BACKGROUND" );
completionHandler( UNNotificationPresentationOptionAlert );
}
else
{
NSLog( #"FOREGROUND" );
completionHandler( UNNotificationPresentationOptionAlert );
}
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadTheTable" object:nil];
}
And I'm listening this notification in ViewController.m like this
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reloadTable:) name:#"reloadTheTable" object:nil];
}
- (void)reloadTable:(NSNotification *)notification
{
// Calling API here
}
This is working fine when the app is running in the foreground.
But not in background and terminate states.
Is there any mistake by me or anything else I have to implement?
From iOS 10, we must add UserNotifications framework and delegate
So first we need to do below things in appDelegate.h
#import <UserNotifications/UserNotifications.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
For FOREGROUND state
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
NSLog( #"Handle push from foreground" );
// custom code to handle push while app is in the foreground
NSLog(#"%#", notification.request.content.userInfo);
}
This is for BACKGROUND state.
So here you need to add the notification
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)())completionHandler
{
NSLog( #"Handle push from background or closed" );
// if you set a member variable in didReceiveRemoteNotification, you will know if this is from closed or background
NSLog(#"%#", response.notification.request.content.userInfo);
//Adding notification here
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadTheTable" object:nil];
}
didReciveRemoteNotificationNotCalled in iOS 10

iOS: Push notification not showing dictionary info in iOS 10

I am working on push notification in XCode 8 Beta, iOS 10 Version. I have got push notification received on device. When I clicked on notification, it fired delegate of UNUserNotificationCenterDelegate, app is opened, but it didn't show any response in userinfo. Do I need to change parameter for sending push in iOS 10 on server side. Below is my code.
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(#"request authorization succeeded!");
}
}];
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
NSLog(#"Notification is triggered");
completionHandler(UNNotificationPresentationOptionAlert);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {
NSLog(#"Tapped in notification");
NSLog(#"%#",response.notification);
NSString *actionIdentifier = response.actionIdentifier;
if ([actionIdentifier isEqualToString:#"com.apple.UNNotificationDefaultActionIdentifier"] ||
[actionIdentifier isEqualToString:#"com.apple.UNNotificationDismissActionIdentifier"]) {
return;
}
}
In iOS 9,
aps = {
alert = "Multi extra param push.";
badge = 0;
sound = default;
};
t = I;
url = "http://www.google.com";
and in iOS 10
alert = "Multi extra param push.";
badge = 0;
sound = default;
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
NSLog(#"%#", notification.request.content.userInfo);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void(^)())completionHandler {
NSLog(#"%#", response.notification.request.content.userInfo);
}

How to get Application states in iOS 10 notification?

I am using
userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler
for getting the notification response in iOS 10, can anyone tell me how to get the Application states in it ?
Because in iOS 9 or before I used
application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
In this method, we can get the application state by
application.applicationState
thanks for the help.
You can get application's state from anywhere in your project something like,
UIApplication *applicaiton = [UIApplication sharedApplication];
if (applicaiton.applicationState == UIApplicationStateBackground) {
NSLog(#"background state");
}
same like you can use UIApplicationStateActive,UIApplicationStateInactive etc to get respective state
I did some search and I got these methods
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
NSLog( #"for handling push in foreground" );
// Your code
NSLog(#"%#", notification.request.content.userInfo); //for getting response payload data
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
NSLog( #"for handling push in background" );
// Your code
NSLog(#"%#", notification.request.content.userInfo); //for getting response payload data
}
Something like this:
NSString * state = #"Unknown";
UIApplication *application = [UIApplication sharedApplication];
if ( application.applicationState == UIApplicationStateInactive ) {
//The application received the notification from an inactive state, i.e. the user tapped the "View" button for the alert.
//If the visible view controller in your view controller stack isn't the one you need then show the right one.
state = #"UIApplicationStateInactive";
}
if(application.applicationState == UIApplicationStateActive ) {
//The application received a notification in the active state, so you can display an alert view or do something appropriate.
state = #"UIApplicationStateActive";
}
if(application.applicationState == UIApplicationStateBackground ) {
state = #"UIApplicationStateBackground";
}

Resources