CM3050 Topic 09: Introduction to APIs
Main Info
Title: Introduction to APIs
Teachers: Joe McAlister
Semester Taken: April 2022
Parent Module: cm3050: Mobile Development
Description
In this module, we learn about advanced APIs, including how to make push notifications with Expo apps.
Key Reading
Other Reading
Lecture Summaries
9.0 Introduction to APIs
APIs are a software interface that provides a service to other software.
Introduces REST APIs, RPCs, and SOAP briefly, better introductions in cm3035: Advanced Web Development.
Push Notifications
Push notifications let users know about items of interest - a new message in their comms app, or a weather change.
People like receiving notifications for important events, some don’t like them at all. People can change their settings on an app or time basis.
If you annoy your user they will withdraw permission. Advice from Apple:
Provide a short title if it provides for the notification content.
Write succinct, easy-to-read notification content. Don’t truncate.
Don’t include sensitive personal, or confidential info.
Avoid sending multiple notifications for the same thing, even if the user doesn’t respond.
Badges are a number to indicate something, such as how many missed messages a user has. Don’t use it for other information.
Don’t use notifications for marketing, unless users explicitly agree to such info.
Using notifications can be very complex, with certifications needed and messages relayed via Apple etc.
Expo simplifies it, creating a token after a user gives permission. This token can be used to send a request to expo via a post request, then expo takes care of the rest. Store the token on the server.
When you export the app, you go get a cert from Apple/Google, then upload that to expo so that they relay the message.
The two expo libraries you need are expo-notifications
(needs installation) and expo-constants
. Import them as Import * as Notifications from 'expo-notifications'
and Import Constants from 'expo-contants'
.
Directly under the imports set the notification settings as follows:
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
Here’s the starter code for requesting permission:
// request the token
useEffect(() => {
(async() => {
let token;
// check, is this a device or a simulator
if (Constants.isDevice) {
// see if we haven't already been granted access
const { status: existingStatus } = await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
if (finalStatus !== 'granted') {
alert('Failed to get push token for push notification!');
return;
}
// ask for the token
token = (await Notifications.getExpoPushTokenAsync()).data;
}else {
alert('You are running this app on a simulator, you must use a real device to use push notifications');
}
// make modifcations to android
if (Platform.OS === 'android') {
Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
}
if (token != undefined) {
console.log(`Our token is ${token}`);
}else {
console.log(`We are unable to get the token`);
}
})();
}, []);
Test the token with the expo web tool or by making a request like this:
fetch('https://exp.host/--/api/v2/push/send', {
method: 'POST',
headers: {
Accept: 'application/json',
'Accept-encoding': 'gzip, deflate',
'Content-Type': 'application/json',
},
body: JSON.stringify(
{
to: EXPO-TOKEN,
sound: 'default',
title: 'Original Title',
body: 'And here is the body!',
data: { someData: 'goes here' },
}
),
});
9.3 Testing
Discusses UAT. Write a list of things you’d like opinions on. Write a list of things that aren’t final and should be ignored by testers.
Consider who will test. Are you asking people who will give you honest opinions?
You can use expo publish
to give people free access. You need to add people to your team.