You are on page 1of 27

iOS 10 Notifications vs

Android Notifications

Cretu Paul Alexandru


Summary

1. About Notifications
2. Managing notifications
3. iOS Notification Images
4. Android Notification Images
5. Creating iOS 10 notification
6. Attaching an Image in the Notification
7. Add remind me later button to iOS 10 notification
8. Creating Android notification
9. Setting your notification priorities in Android
10. Bibliography
About Notifications

Apps can use notifications to provide timely and important information anytime, whether the
device is locked or in use. For example, notifications may occur when a message has arrived, an
event is about to occur, new data is available, or the status of something has changed.

iOS Android
Managing notifications
The behavior of notifications is managed in Settings on an app-by-app basis. For any app that
supports notifications, you can enable or disable the feature entirely.

iOS Android

 You can also enable visibility in Notifications can take different forms:
Notification Center and on the lock  A persistent icon - that goes in the
screen, enable app icon badging, and status bar and is accessible through the
choose one of these notification styles: launcher, (when the user selects it, a
 Banner. Appears at the top of the designated Intent can be launched),
screen for a few seconds while the  Turning on or flashing LEDs on the
device is in use, then disappears. device
 Alert. Appears at the top of the screen  Alerting the user by flashing the
while the device is in use and stays backlight, playing a sound, or
there until manually dismissed. vibrating.
iOS Notification Images

Banner Alert
Android Notification Images
Creating iOS 10 notification

The process for creating a notification has changed slightly in iOS 10. Developers must now create
requests, which are requests to iOS to present a notification. These requests consist of 2
components: triggers and content. A trigger is a set of conditions that must be met for a
notification to deliver. To show a notification in iOS 10, we begin with a trigger. The class we use
for date based triggers is UNCalendarNotificationTrigger. Apple provides a few others, such as
UNLocationNotificationTrigger (which triggers a notification when the user arrives a particular
location). All the notification-related APIs are organized in this framework:

import UserNotifications
Creating iOS 10 notification

The first step to displaying a notification on iOS is creating a trigger. Let’s go ahead and make one
now. Like most other things in iOS, Apple has made this process pretty easy. Just add the
following line of code in the function:

let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)

There is set repeats to false because we only want the notification to be appeared once.
Creating iOS 10 notification

Now that we have created a trigger, the next thing is to create some content to display in our
notification. This can be done through the UNMutableNotificationContent class.

let content = UNMutableNotificationContent()


content.title = "Tutorial Reminder"
content.body = "Just a reminder to read your tutorial over at appcoda.com!"
content.sound = UNNotificationSound.default()
Creating iOS 10 notification

To create a notification request, we should instantiate a UNNotificationRequest object with the


corresponding content and trigger. We also need to give it a unique identifier for identifying this
notification request.

let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger:


trigger)

Easy, right? We create a request with 3 parameters:

• identifier: This is a unique identifier for our request. As you’ll see shortly, this identifier can be
used to cancel notification requests.

• content: This is the notification content we created earlier.

• trigger: This is the trigger we would like to use to trigger our notification. When the conditions
of the trigger are met, iOS will display the notification.
Creating iOS 10 notification
Creating iOS 10 notification

Okay, the last thing we have to do is add the request to the notification center that manages all
notifications for your app. The notification center will listen to the appropriate event and trigger
the notification accordingly.

let selectedDate = sender.date


let delegate = UIApplication.shared.delegate as? AppDelegate
delegate?.scheduleNotification(at: selectedDate)
Creating iOS 10 notification
After all these steps, the notification should look like:
Attaching an Image in the Notification

The notification is now in text-based. This is nothing new! Let’s explore some other features, like
displaying an image in a notification.

if let path = Bundle.main.path(forResource: "logo", ofType: "png") {


let url = URL(fileURLWithPath: path)
do {
let attachment = try UNNotificationAttachment(identifier: "logo", url: url, options: nil)
content.attachments = [attachment]
} catch {
print("The attachment was not loaded.")
}
}
Attaching an Image in the Notification
Add remind me later button to iOS 10 notification

This is pretty cool, but I think it will be even better if we can add a “remind me later” button that
allows users to temporarily ignore a reminder. Let’s do that now.

let action = UNNotificationAction(identifier: "remindLater", title: "Remind me later", options: [])


let category = UNNotificationCategory(identifier: "myCategory", actions: [action], intentIdentifiers: [],
options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
Add remind me later button to iOS 10 notification
Creating Android notification

Notifications in Android are represented by the Notification class. To create notifications you use
the NotificationManager class which can be received from the Context, e.g. an activity or a
service, via the getSystemService() method.

NotificationManager notificationManager = (NotificationManager)


getSystemService(NOTIFICATION_SERVICE);

The Notification.Builder provides an builder interface to create an Notification object.


Creating Android notification

We will use Notification Builder to set various Notification properties like its small and large icons,
title, priority etc. But this is mandatory to set at least following:

• A small icon, set by setSmallIcon()


• A title, set by setContentTitle()
• Detail text, set by setContentText()
Creating Android notification
Creating Android notification
Creating Android notification

Android 4.1 supports expandable notifications. In addition to normal notification view it is


possible to define a big view which gets shown when notification is expanded. There are three
styles to be used with the big view: big picture style, big text style, Inbox style. The following code
demonstrates the usage of the BigTextStyle() which allows to use up to 256 dp.

String longText = "...";


Notification n = new Notification.Builder(this).
.....
.setStyle(new Notification.BigTextStyle().bigText(longText))
Creating Android notification
Creating Android notification
Setting your notification priorities in Android

Do you feel like multiple notifications are always vying for your attention? Maybe your
smartphone is constantly buzzing, ringing, or flashing its LED lights, to the point where new
notifications aren’t even something you get excited about anymore?

.setPriority(Notification.PRIORITY_MAX)

If you don’t assign a priority to your notification, it’ll automatically be assigned


PRIORITY_DEFAULT. Alternatively, you can let the Android system know exactly how important
your notification is, using the setPriority() method
Bibliography

• http://www.appcoda.com/ios10-user-notifications-guide/
• https://developer.apple.com/notifications/
• www.vogella.com/tutorials/AndroidNotifications/article.html
• http://www.androidauthority.com/how-to-create-android-notifications-707254/
• https://www.tutorialspoint.com/android/android_notifications.htm
http://www.appcoda.com/ios10-user-notifications-guide/
https://developer.apple.com/notifications/
www.vogella.com/tutorials/AndroidNotifications/article.html
http://www.androidauthority.com/how-to-create-android-notifica
tions-707254/
https://www.tutorialspoint.com/android/android_notifications.ht
m

You might also like