I want to send a onesignal notification from a button in android studio to another device using device tokens. But I am not getting it. What is the proper way of doing?
I want event to be trigger by the button not by onesignal dashboard
SendNotificationBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//code to send notification to another device.
}
});
Use OneSignal REST API (see Send to Specific Devices in the documentation)
Related
Here is my requirement in project :
My app is for a media posts, post can be edited. On edit post, i want to send notification to all those users who subscribed to the post notification.
subscription code :
On click of subscribe button flutter app will execute below code.
await firebaseMessaging.subscribeToTopic(uniqueIdOfPost);
along with subscribe, app will also store information in firestore so that user can unsubscribe if required.
Code to send notification :
Here is my admin sdk backend script in nodejs (for now its backend script later i will convert it into cloud function)
var admin = require("firebase-admin");
var serviceAccount = require("/DriveA/firebase/adminsdk/firebase-adminsdk.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
var payload = {
notification: {
title: "Notification from Topic",
body: "This is the body of the notification message."
}
};
// This is unique ID of post.
var topic = "xpxpwd9856lsktppw";
admin.messaging().sendToTopic(topic, payload)
.then(function(response) {
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
suppose a case where user logged in into the app and subscribed to notification. On post edit my backend code runs and it sends notification to device.
So far so good as i am using single device for testing.
If the same user logs in from other device that device is not receiving notification. (may be because i subscribed to topic from other device).
Here is my question : how should i use topic subscription to handle this multi-device use case of a user ? does my use case can be solved using topic subscription or there is any other approach ? it will be good if if someone points me the code which handle this use case.
Sorry, It won't work when the user starts to subscribe new topic from one of the phones, the other phone won't get the notification.
What I do instead of the user(a device) subscribe to a topic, I subscribe the device to the user id(uid) topic.
Then, when a user subscribes to a topic, I add the uid to the subscription field of the posting in firebase. Then when you need to send notification relate to a posting, read the subscription field and send notification to the uids in the field.
Here is what I did.
1. A user login
- subscribeToTopic(uid)
2. When user subscribe to a posting
- add uid to subscribe field
3. When sending notification
- read uids from subscribe field
- for all uids, `sendToTopic(uid)`
4. A user logout
- unsubscribeToTopic(uid)
I am implementing the push notifications in my xamarin.forms app using azure notification hub following this documentation from Microsoft. The implementation works fine and I am able to receive notifications on both android and ios devices. I was testing some use cases and came across the issue with token retrieval in Android if for some reason the app crashes before the device registers with Azure notification hub. Below is my firebase service code.
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class FirebaseService : FirebaseMessagingService
{
readonly AndroidHelper androidHelper = new AndroidHelper();
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
androidHelper.SendLocalNotification(message.Data);
}
//Send pns handle for device installation with Azure Notification Hub.
public override void OnNewToken(string token)
{
PushNotifications.PNSHandle = token;
// APP CRASH OCCURS
}
}
Whenever the token is generated, I store it in a static variable and use it for the device registration later.
But since OnNewToken() only runs when the app is launched first time after install. If for some reason let's say I get a crash right after I assign the token to my static variable as mentioned in the comment above inside OnNewToken() and I restart the app, I don't get that token again.
Can anyone please help with token retrieval or regeneration again on every app launch. After searching I came across this post from xamarin forums but not able to get what I am looking for.
I use nativescript-vue and this plugin for sending push notifications:
nativescript-plugin-firebase,
How do i send an image along when i send a push notification?
In my nativescript-vue project i receive the notification like this:
firebase.init({
showNotificationsWhenInForeground: true,
onMessageReceivedCallback: async (message) => {
})
How do i show an image in the notification?
I have tried with including image: "url" and that dosnt work. I think there is way to show a image in the notification because on the firebase panel there is an option to send along a image.
The firebase notification plugin only acts as a receiver and handler of notifications in this case. If you want to send a remote notification with image, you are going to do that with your notification sender; and in this case on Firebase.
If you just want to quickly send or test a push notification with an image, you can do so by going to Firebase Console > Cloud Messaging.
Then just add your image in the respective field.
Now, in most cases in production you would want to do this progmatically on your backend instead of going to Firebase Console. You can do that by using Firebase FCM SDK.
In our case, we usually do it via API calls something like:
POST https://fcm.googleapis.com/fcm/send
BODY
{
"to": "/topics/user_{{userId}}",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message"
}
}
Header should include a key "Authorization" with value:
key=<YOUR_FCM_KEY_HERE>
You can get your FCM key by going to Firebase Console > Project Settings (the gear) > Cloud Messaging. You can check this doc for more details on sending.
Lastly, if you really want to handle the notification image via your app (not suggested), you can force so by using a local push notification by force handling the remote push notification and doing a local push notification with your own desired image.
I have developed an Xamarin Android app that successfully do the following things
Can register with Baidu Push
Can receive notification from Baidu Push
Can register with Azure Notification Hub (I can see registration record in the Azure Notification Hub dashboard, but I can see no way to see the detail behind these count)
However when testing with "Test Send" function in Notification Hub, my app won't be able to receive notification.
I confirm that I have put the correct Api Key and Secret Key in Notification Hub > Baidu setting.
So I am stuck in at this point. Anyone have any past experience on this? Appreciate any support / comment / sharing. Thanks!
P.S. I can only see registration count increasing, but I have no way to ensure my registration of device is under Baidu. How can Azure Notification Hub be able to distinguish that my registration token is related to Baidu? The registration code is just a few lines as below
// Register with Notification Hubs
hub = new NotificationHub("Notification_Hub", "MY_HUB_CONNECTION_STRING", context);
var tags = new List<string>() { };
var regID = hub.Register(token, tags.ToArray()).RegistrationId;
Console.WriteLine($"Successful registration of ID {regID}");
Sorry I made a mistake, I should use the following function for Baidu registration. Hope it help some novice like me :o)
var regID = hub.RegisterBaidu(userId, channelId, someTags);
Currently I am using the fcm plugin to send push notifications to my ionic 3 app. I am also able to push and view specific pages when the notifications are tapped with the ability to return to the homepage while the app is in the background.
fcm.onNotification().subscribe(data=>{
if(data.wasTapped){
if(data.type == "created"){
this.nav.push('PostsolutionPage', {key : data.IssueID});
}
} else {
if(data.type == "created"){
//show Toast
}
}
});
However if I tap a notification while the app isn't in the background. The page I want to load appears However I cannot return to the homepage(RootPage) again. Is there any way to resolve this?
(I don't know whether this is required but I set my Rootpage to a loginpage or a homepage by subscribing to a firebase auth instance and navigating accordingly)