Send push Notification in Ionic - android

I am working on an ionic application and need to implement push notifications using the back end API's developed in .NET.
While going through few blogs I found fire base API's and was able to complete a POC using Firebase. However the notification did not show when the app was in foreground.
I am not sure how to consume .NET API's to get the push messages. Can we achieve this without using Firebase. Please suggest. Thanks !

It is normal so that notification not show on forground using firebase notifications.
this.firebase.onNotificationOpen().subscribe( (msg) => {
if (this.platform.is('ios')) {
this.presentToast(msg.aps.alert);
//here notfication in ios
} else {
this.presentToast(msg.body);
//here notfication in android
}
});
This will trigger a toast of notification in foreground. If you want it as notfication, use local notfication and set the msg.body details as it should be shown inside the local notfication.
And as an advice, use onesignal notfication since it contains better features plus easier and more flexible.

Related

How to pass data from background android service to react-native

I am building an react-native app. I have a background android service that receives a push notification. I need to store the notification information(title, description...) on a react-native database or in asyncstorage... because in my app I have a page that shows all the notifications. I need that If my app is on background and i receive a notification, when the user open the app the page shows this information. How I can do this?
I try to store the notification on the same database that react-native uses but I can't store the data on the android service. Please, help me.
Did you try exploring these.
componentWillMount() {
Linking.addEventListener("url", this.handleOpenURL);
OneSignal.addEventListener("ids", this.onIds);
OneSignal.addEventListener("received", this.onReceived);
OneSignal.addEventListener("opened", this.onOpened);
}
And in oneReceived event listener you can store data in async storage received from push notification.
onReceived(data) {
this.props.setData(data);
}
In set data you can have your logics to store data.Well i must say push notifications are not used to fetch data and to store and then to render.These provide just little information to open specific screen .Like i received a notification related to deals.Then pressing upon will automatically opens deals screen through linking not the home page of app.For your case you can send the request on that specific page to fetch data.
Yes, but when you close the app and after this, someone sends one push notification, the event received and opened etc don't do nothing. I tried to make a custom native module that receives this info and try to send an Event. I followed this guide:
https://facebook.github.io/react-native/docs/native-modules-android#sending-events-to-javascript
But it don't works, always returns "sendEvent called before bundle loaded" java.lang.NullPointerException.

Foreground app notification is almost empty, background app gets notification but without custom data

I'm working on an Android app in Kotlin and I want to send notification in order to click on it and open a custom view.
I've implemented notification thanks to Firebase. I can send notification thans to Firebase, but the notification are well displayed (with the right icone (= triangle), the right title and the right message) only if the app is in background, but the custom data (key => val) provided in firebase are not detected.
For the foreground app, if I send the notification from Firebase, I can display the custom data (key => val), but the notification doesn't have title, message or custom icone (it has the square/rounded icone)
Instead of showing all my code, I prefere to give you the link I've followed.
https://www.android4dev.com/firebase-pushnotification-android/
I want to have the same result on both side so I can get the custom data.
See the result :
There are two types of FCM messages and the behaviour changes based on it.
Notification messages (these are handled by the SDK automatically)
Data messages (handled by the app)
Use notification messages when you want FCM to handle displaying a
notification on your client app's behalf. Use data messages when you
want to process the messages on your client app.
https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
I think that you should use data messages instead of notification messages to have a consistent behaviour.

how to do more with ionic push (ionic.cloud/$ionicPush)

I am building an ionic app using sublime because I find it to be more simpler than android studio.
currently I am addion push notification using ionic.cloud with fcm to send push notifications
I foolowed the instructions here: https://docs.ionic.io/services/push/
currently the notifications are working but I want to add more functionality namely
do specific action when notification is received in background/foreground
access payload
the snippet below is shows some code from the link above. are there additional functions for the ionicPush object or is there another way to achieve the functionality stated above?
ionicPush.register().then(function(t) {
return $ionicPush.saveToken(t);
}).then(function(t) {
console.log('Token saved:', t.token);
});

iOS Firebase Cloud Messaging Conflicting With Android

I am writing a iOS and Android application that is supposed to notify users through the FCM API yet there seems to be a catch-22 within the firebase API that involves both platforms:
On Android, to handle new notifications using a custom implementation of FirebaseMessagingService.onMessageReceived(), one must create a DATA only message server-side. E.g: { "data": {"param": "foo" },"to" : "/topics/bar"}. The moment a "notification" field is included in the json, the custom implementation will not be called when the app is in the background. This would be ok but...
On iOS, if the "notification" field is not included in the message, the message just isn't received when the app is in the background which is unacceptable.
Thus, to receive background notification on iOS, you need to add the "notification" field, but adding this field makes the Android apps display bogus, non-customizable notifications when in the background!
I hope there is something that I am missing in the documentation. I would like to know how others have overcome this problem and whether it would be wise to stick with FCM for iOS or ditch it for something else.

Push Messages for PhoneGap

I created an app for iPhone and Android using Phonegap.
Now I wanted to add push functionality which already works pretty good.
When a push message arrives on my iPhone I get a message on the homescreen. If I swipe it, iOS will open my application. - So far so good.
Now, within my PhoneGap app I need to check what that message actually says in order to open the correct view within my app via JavaScript.
I know there are quite some posts about this but I couldn't find some clear answers to these questions:
Does PhoneGap support push messages?
If yes, where is the documentation for that?
If not, which plugins/frameworks can be recommended? So far I found pushwoosh and Urbanair. Are they any good?
Regarding Pushwoosh, I noticed that I need some kind of pushwoosh ID - Why that?
By Pushwoosh ID you most probably mean Pushwoosh App Id or Application Code. It's an ID of your application in Pushwoosh Control Panel (current format is XXXXX-XXXXX). You will see it as soon as you add a new app in Pushwoosh.
There was quite an extensive blog post made by Holly Schinsky on easy PhoneGap integration with Pushwoosh
http://devgirl.org/2012/12/04/easy-phonegap-push-notifications-with-pushwoosh/
It should be very helpful for all PhoneGap developers aiming to integrate push notifications into their apps.
I found a really easy solution without using a framework.
I simply added the following code to the very end of the method didFinishLaunchingWithOptions (Right before the return statement):
if (launchOptions != nil)
{
NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
NSLog(#"Launched from push notification: %#", dictionary);
[self addMessageFromRemoteNotification:dictionary updateUI:NO];
}
}
Also, I added this new method to my AppDelegate.m which gets the payload from the push message:
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
NSLog(#"Received notification: %#", userInfo);
[self addMessageFromRemoteNotification:userInfo updateUI:YES];
}
(The NSLog calls above will show you what's inside the push message)
As a last step, I added this listener to do what ever I want with the payload:
- (void)addMessageFromRemoteNotification:(NSDictionary*)userInfo updateUI:(BOOL)updateUI
{
// do what ever you want with userInfo
}
If you want to do an additional JavaScript call with this info, I recommend using:
[self.viewController.webView stringByEvaluatingJavaScriptFromString:#"callSomeJSFunction()"];
Of course you can also pass arguments as strings into that JS function if you want.

Categories

Resources