Flutter save OneSignal notification message - flutter

I want to save the message from OneSignal push notification, so I can create a notification list.. but I have no idea to achieve this.. I open the Onesignal for flutter documentation but still no clue.. anyone can share some references to me?

In your main.dart make sure you have the listener setNotificationReceivedHandler activated. Then just save the notification.payload elements
OneSignal.shared.setNotificationReceivedHandler((notification) {
this.setState(() {
_debugLabelString =
"Received notification: \n${notification.jsonRepresentation().replaceAll("\\n", "\n")}";
newNotificationTitle = notification.payload.title;
newNotificationBody = notification.payload.body;
});
});

Related

How do I show / reset cancelled local notification in Flutter?

I am using flutter_local_notifications 12.0.2 I have a code which will show a notification and after I wanted to cancel it
But I have an if statement which I would like to trigger the notification back or uncancel it, is there any way I can achieve that?
P.S: I tried initializing it and calling .pendingNotificationRequests() but its not working
here is my code:
if (Geolocator.distanceBetween(newLoc.latitude!, newLoc.longitude!,
des.latitude, des.longitude) <
200) {
isNear = true;
NotificationApi.showBigTextNotification(
title: "IMPORTANT",
body: "Dont forget to visit our shop!",
fln: flutterLocalNotificationsPlugin);
flutterLocalNotificationsPlugin.cancel(0);
} else {
isNear = false;
NotificationApi.initialize(flutterLocalNotificationsPlugin);
flutterLocalNotificationsPlugin.pendingNotificationRequests();
}

How to unschedule previously scheduled local notifications after some time?

I am using flutter_local_notifications library to schedule local notifications every 1 hour. It is working as expected but now, I need a way to start/ stop the notification schedule (say, at push of a button).
I could not find anything regarding canceling scheduled notification requests in the documentation.
Cancelling/deleting a notification
// cancel the notification with id value of zero
await flutterLocalNotificationsPlugin.cancel(0);
// 0 is your notification id
Cancelling/deleting all notifications
await flutterLocalNotificationsPlugin.cancelAll();
And yes, this can cancel current and future notification. Just make sure correct notification id.
In the docs it is given in Cancelling/deleting a notification
await flutterLocalNotificationsPlugin.cancel(0);
I would do something like this:
final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
// Below will give you any unpresented/scheduled notifications
final List<PendingNotificationRequest> pendingNotificationRequests =
await _flutterLocalNotificationsPlugin.pendingNotificationRequests();
for (var _pendingRequest in pendingNotificationRequests) {
_flutterLocalNotificationsPlugin.cancel(_pendingRequest.id);
}
Here _pendingRequest.id will give you the required notification id to cancel the specific notification request.
This is tested in Android. Will update the status on iOS soon.
await flutterLocalNotificationsPlugin.cancelAll(); //cancel future note
await flutterLocalNotificationsPlugin.pendingNotificationRequests(); //restart note
And your notification will show again.
If you want to cancel all notifications, then you have to do that using the following:
await flutterLocalNotificationsPlugin.cancelAll();
Also, if you want to cancel a specific notification, you can do this by using a specific ID of your notification:
await flutterLocalNotificationsPlugin.cancel(0); // 0 is your notification id
Where this notification ID came from ??
While you are declaring your notification to show on your screen you have to add some data like given below:
await _flutterLocalNotificationsPlugin.show(
0, // this is the notification id
message.notification.title,
message.notification.body,
notificationDetails,
payload: jsonEncode(message.data),
);
Here I am using Flutter Local Notification Plugin to show notifications!!!
Hope this will solve your problem.

Initialize OneSignal SDK on Ionic with React.js

I'm trying to start with OneSignal SDK on a Ionic with React app, but I can't find any guide for React, all I find is for angular and I'm not pratice of typescript.
Where do I need to initialize OneSignal SDK?
I'm trying to do it in the App.componentDidMount such as:
componentDidMount() {
this.props.oneSignal.startInit(
"XXXXXX-XXX-XXX-X-XXXXX",
"YYYYYYYYYY"
);
this.props.oneSignal.inFocusDisplaying(
this.props.oneSignal.OSInFocusDisplayOption.InAppAlert
);
this.props.oneSignal.handleNotificationReceived().subscribe(() => {
// do something when notification is received
});
this.props.oneSignal.handleNotificationOpened().subscribe(() => {
// do something when a notification is opened
});
this.props.oneSignal.endInit();
}
but the IDE continues giving me errors about this.oneSignal does not exists in the type xxx.
My fault.
I was importing this way:
import { OneSignal } from '#ionic-native/onesignal/ngx';
This the correct way for React.js:
import { OneSignal } from '#ionic-native/onesignal';

Flutter open whatsapp with text message

I want to open whatsapp from my Flutter application and send a specific text string. I'll select who I send it to when I'm in whatsapp.
After making some research I came up with this:
_launchWhatsapp() async {
const url = "https://wa.me/?text=Hey buddy, try this super cool new app!";
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
Which works ok ish, however there are two problems:
As soon as I make the text string into multi words it fails. So if I change it to:
_launchWhatsapp() async {
const url = "https://wa.me/?text=Hey buddy, try this super cool new app!";
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
Then the Could not launch $url is thrown.
I have whatsapp already installed on my phone, but it doesn't go directly to the app, instead it gives me a webpage first and the option to open the app.
Here is the webpage that I see:
Any help on resolving either of these issues would be greatly appreciated.
Thanks
Carson
P.s. I'm using the Url_launcher package to do this.
From the official Whatsapp FAQ, you can see that using "Universal links are the preferred method of linking to a WhatsApp account".
So in your code, the url string should be:
const url = "https://wa.me/?text=YourTextHere";
If the user has Whatsapp installed in his phone, this link will open it directly. That should solve the problem of opening a webpage first.
For the problem of not being able to send multi-word messages, that's because you need to encode your message as a URL. Thats stated in the documentation aswell:
URL-encodedtext is the URL-encoded pre-filled message.
So, in order to url-encode your message in Dart, you can do it as follows:
const url = "https://wa.me/?text=Your Message here";
var encoded = Uri.encodeFull(url);
As seen in the Dart Language tour.
Please note that in your example-code you have put an extra set of single quotes around the text-message, which you shouldn't.
Edit:
Another option also presented in the Whatsapp FAQ is to directly use the Whatsapp Scheme. If you want to try that, you can use the following url:
const url = "whatsapp://send?text=Hello World!"
Please also note that if you are testing in iOS9 or greater, the Apple Documentation states:
Important
If your app is linked on or after iOS 9.0, you must declare the URL schemes you pass to this method by adding the LSApplicationQueriesSchemes key to your app's Info.plist file. This method always returns false for undeclared schemes, whether or not an appropriate app is installed. To learn more about the key, see LSApplicationQueriesSchemes.
So you need to add the following keys to your info.plist, in case you are using the custom whatsapp scheme:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
till date: 27-06-2022
using package: https://pub.dev/packages/url_launcher
dependencies - url_launcher: ^6.1.2
TextButton(
onPressed: () {
_launchWhatsapp();
},
)
_launchWhatsapp() async {
var whatsapp = "+91XXXXXXXXXX";
var whatsappAndroid =Uri.parse("whatsapp://send?phone=$whatsapp&text=hello");
if (await canLaunchUrl(whatsappAndroid)) {
await launchUrl(whatsappAndroid);
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("WhatsApp is not installed on the device"),
),
);
}
}
Here is new update way...
whatsapp() async{
var contact = "+880123232333";
var androidUrl = "whatsapp://send?phone=$contact&text=Hi, I need some help";
var iosUrl = "https://wa.me/$contact?text=${Uri.parse('Hi, I need some help')}";
try{
if(Platform.isIOS){
await launchUrl(Uri.parse(iosUrl));
}
else{
await launchUrl(Uri.parse(androidUrl));
}
} on Exception{
EasyLoading.showError('WhatsApp is not installed.');
}
}
and call whatsapp function in onpress or ontap function
For using the wa.me domain, make sure to use this format...
https://wa.me/123?text=Your Message here
This will send to the phone number 123. Otherwise, you will get an error message (see? https://wa.me/?text=YourMessageHere ). Or, if you don't want to include the phone number, try this...
https://api.whatsapp.com/send?text=Hello there!
Remember, wa.me requires a phone number, whereas api.whatsapp.com does not. Hope this helps!
I know it is too late for answering this, but for those who want the same functionality, the current way is to do it like this:
launchUrl(Uri.parse('https://wa.me/$countryCode$contactNo?text=Hi'),
mode: LaunchMode.externalApplication);
if you will use URL Launcher then the whatsapp link will be open on web browser. So you need to set parameter - not to open on safari browser. The complete code you can find on this flutter tutorial.
But for your case use below code.
await launch(whatappURL, forceSafariVC: false);
Today i am adding solution its working fine on my desktop and phone
Add 91 if your country code is +91
Remember not add any http or https prefix otherwise wont work.
whatsapp://send?phone=9112345678&text=Hello%20World!

Ionic FCM push notifications observable does not emit

I am doing the following in an ionic application:
When a user is logged in, subscribe to their user ID topic
When a push notification arrives, console.log something.
this.authState.subscribe((state) => {
if (state) {
this.fcmPush.subscribeToTopic(state.uid);
this.fcmPush.onNotification().subscribe(notification => {
if (notification.wasTapped) {
console.log('Received in background', notification);
} else {
console.log('Received in foreground', notification);
}
});
}
});
Source: https://github.com/AmitMY/ionic-starter-firebase/blob/master/src/app/app.component.ts#L118
Sending a notification to my own topic arrives (after a few minutes), but I never see anything in console, both from outside the app, and inside the app.
What am I doing wrong?
Sending a notification from firebase messaging does not work.
However, using the sdk I must add:
click_action: "FCM_PLUGIN_ACTIVITY",
Which is in the usage guide but I missed