Ionic Local Notifications vs Push Notifications


#1

Hi All,

I am using Ionic 2 to build a Chat App. The Chat App is using Ionic and Meteor and works perfectly. The app has an event that is fired when a new message appears (when Meteor updates the Mongo cursor).

I am considering using either Local Notifications or Push Notifications. I would prefer Local Notifications, because they are simpler and I already have an event when I need a notification.

Question

My question however is, if the app is not running, will it still be able to display a notification if I use Local Notification?


#2

HI @richardmarais, In my opinion, local notification will be shown even your app is not running, because when i played some mobile game, my mobile didn’t connect internet, but i still received notification


#3

It depends. In short, a local notification has to be triggered or scheduled from within your application, a push notification can be triggered from outside of your application.

In your case, since an event fires inside of your app whenever a message is received then yes you could use that to trigger a local notification from inside your application. This will also work if the app is closed and you are using something like the background plugin, so that your app continues to run in the background. However, if the app is completely closed (not just running in the background), then local notifications can only trigger notifications that have been scheduled before the app was closed.

So if you want the user to get a notification whenever a new message occurs, even if they have the app completely closed, then you will need to use push notifications.


#4

Hey Josh !
Thanks for all your tutorials, those are really helpful :slight_smile:
Did you write a tutorial about how to make push notifications work with ionic 2?
Or maybe you are planning to?


#6

Not sure if Josh did, but he makes some great tutorials so when he does I’m sure it will be the best out there. In the meantime, I did a small write up on how to implement Ionic push notifications

This is part one about how to integrate their service into your app.


#7

Hi All,

I am using Ionic 3 with Ionic Native Push Notifications on the client and Java com.notnoop.apns on the server.

I can get the Push Notifications to work successfully on an Android devise. However, on an iOS devise, the client does not display the notification.

client (ts)

        let topics: string[] = [this.personModelLoggedIn.uid];
        const options: PushOptions = {
          android: {
            senderID: "XXXXXXXXXXX",
            sound: "true",
            vibrate: "true",
            topics: topics
          },
          ios: {
            senderID: "XXXXXXXXXXX",
            alert: "true",
            badge: true,
            sound: "true"//,
            //topics: topics
          },
          windows: {}
        };
        const pushObject: PushObject = this.push.init(options);


        pushObject.on('notification').subscribe((data: any) => {
          alert('Received Notification!!! message = ' + data.message);
        });

server (java)

Android

private String sendAndroidPushNotification(String device_token, String topics, String title, String message)
		throws Exception {
	String pushMessage = null;
	if (device_token != null && !device_token.equals("null")) {
		pushMessage = "{\"data\":{\"title\":\"" + title + "\",\"message\":\"" + message + "\"},\"to\":\""
				+ device_token + "\"}";
	} else {
		pushMessage = "{\"data\":{\"title\":\"" + title + "\",\"message\":\"" + message + "\"},\"to\": \"/topics/"
				+ topics + "\"}";
	}
	// Create connection to send FCM Message request.
	URL url = new URL("https://fcm.googleapis.com/fcm/send");
	HttpURLConnection conn = (HttpURLConnection) url.openConnection();
	conn.setRequestProperty("Authorization", "key=" + SERVER_KEY);
	conn.setRequestProperty("Content-Type", "application/json");
	conn.setRequestMethod("POST");
	conn.setDoOutput(true);
	// Send FCM message content.
	OutputStream outputStream = conn.getOutputStream();
	outputStream.write(pushMessage.getBytes());

	return "Android Push Notification: " + conn.getResponseCode() + " " + conn.getResponseMessage() + " - " + pushMessage;
}

iOS

private String sendIOSPushNotification(String device_token, String topics, String title, String message)
		throws Exception {
	ApnsService service = APNS.newService().withCert(PATH_TO_P12_CERT, CERT_PASSWORD).withSandboxDestination()
			.build();

	String payload = APNS.newPayload()
			// .customFields(map)
			.alertBody(title + " " + message).sound("default").build();

	//service.push(Utilities.encodeHex(topics.getBytes()), payload);
	service.push(Utilities.encodeHex(device_token.getBytes()), payload);
	return "iOS Push Notification: " + title + " " + message;
}

When the above two java methods get called with the appropriate devise tokens, the Android devise receives a notification, but the iOS devise does not.

Question

If anyone can advise what’s wrong with my iOS code (either client or server), I would appreciate your advise.


#9

Do you use push notifications and local notifications together?
I found out, the they dont work together in one project on iOS>10…