Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Intercept push notification

I'm making an applikation which is using push notifications, I am currently using pushbots for the push notifications. I was wondering if there is any way to intercept the notifications that is received by the application and check the notification before the notification is shown on the device. And if the data in the notification is not correct, dont show a notification at all? Is this possible with pushbot or do I need to do it all by my self?

like image 458
Uffe Avatar asked Jun 17 '14 17:06

Uffe


2 Answers

Yes, you can achieve this behaviour playing around with Local Notifications.

You can configure your payload without an alert and "content-available": "1" so your application can receive notifications without showing them to the user.

// Payload
{
    aps: {
       "content-available": 1
    },
    text: 'my alert message' // your custom info
} 

And in your app code, register notifications as

// Register notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
 UIRemoteNotificationTypeNewsstandContentAvailability];

Then, the key is to trigger Local Notifications in the application:didReceiveRemoteNotification:fetchCompletionHandler: method based on some condition

- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler 
{

    NSLog(@"push data package: %@", userInfo);

    // Retrieve your data        
    NSString *text = [userInfo objectForKey:@"text"];

    BOOL mustShow = YES;
    // Only show notification if app is background and your custom condition
    if ((state == UIApplicationStateInactive || state == UIApplicationStateBackground) 
         &&  mustShow) {
        // Raise the local notification a second after received
        UILocalNotification* localNotification = [[UILocalNotification alloc] init];
        localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
        localNotification.alertBody = text;
        localNotification.timeZone = [NSTimeZone defaultTimeZone];
        localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    }
}

This way you can "intercept" the notifications before "displaying" them to the user.

like image 126
Agustin Meriles Avatar answered Oct 20 '22 22:10

Agustin Meriles


You can't prevent the notification from being shown once it reaches the device (assuming it contains an alert field in the aps dictionary - if it doesn't, no notification will be shown anyway).

You should determine in your server which notifications should be sent to which device tokens. You can associate device tokens with users in your DB, if your functionality requires it.

Even if what you request was possible, it would be very inefficient to send notifications to all the devices that installed your app, and then only display the notification in a small subset of them.

like image 25
Eran Avatar answered Oct 20 '22 23:10

Eran