I am sending push notification from iOS and Android both by calling a cloud function, and in each device I am getting 5 times a single push notification. I am using parse database hosted on back4app.
Cloud code is given below:
Parse.Cloud.define("push", function (request, response) {
var query = new Parse.Query(Parse.Installation);
var userID = request.params.user;
var message = request.params.message;
var notificationType = request.params.notificationType;
var user = new Parse.User();
user.id = userID;
query.equalTo('user', user);
query.equalTo("allowPush", true);
Parse.Push.send({
where: query,
data: {
alert: message,
sound: 'default',
"type": notificationType
}
}, { useMasterKey: true });
});
Try to call reponse.success and response.error functions in the end of your cloud code function. Since your client code is not receiving the feedback if the call worked or not it is probably attempting to send again.
Parse.Cloud.define("push", function (request, response) {
var query = new Parse.Query(Parse.Installation);
var userID = request.params.user;
var message = request.params.message;
var notificationType = request.params.notificationType;
var user = new Parse.User();
user.id = userID;
query.equalTo('user', user);
query.equalTo("allowPush", true);
Parse.Push.send({
where: query,
data: {
alert: message,
sound: 'default',
"type": notificationType
}
},
{
success: function () { response.success(); },
error: function(err) { response.error(err); },
useMasterKey: true
});
});
Related
I am trying to pass a custom object which is of type User from native platform to Flutter. The User class is part of a library and not accessible directly for editing. Here is my android and iOS code implementation for the same. Problem is I am not able to find a solution on how to pass this object through method channels in such a way that I can parse it in the Dart code easily.
Android part:
private fun loginUser(uid: String, apiKey: String, result: MethodChannel.Result) {
MyChat.login(uid, apiKey, object : MyChat.CallbackListener<User>() {
override fun onSuccess(user: User) {
Log.e(TAG, user.toString())
result.success(hashMapOf("RESULT" to true, "AVATAR" to user.avatar,
"CREDITS" to user.credits,
"EMAIL" to user.email,
"LAST_ACTIVE" to user.lastActiveAt,
"NAME" to user.name,
"ROLE" to user.role,
"STATUS" to user.status,
"STATUS_MESSAGE" to user.statusMessage).toString())
}
override fun onError(p0: MyChatException?) {
Log.e(TAG, p0?.message)
result.error("FAILED", "Unable to create login", null)
}
})
}
iOS implementation:
func loginUser(result: #escaping FlutterResult, uid: String, apiKey: String){
MyChat.login(UID: uid, apiKey: apiKey, onSuccess: { (user) in
// Login Successful
let data: [String: Any] = ["RESULT":true,
"AVATAR":user.avatar!,
"CREDITS": user.credits,
"EMAIL": user.email!,
"LAST_ACTIVE":String(user.lastActiveAt),
"NAME":user.name!,
"ROLE":user.role!,
"STATUS":user.status.rawValue,
"STATUS_MESSAGE":user.statusMessage]
let jsonData = try? JSONSerialization.data(withJSONObject: data, options: [.prettyPrinted])
result(String(data: jsonData!, encoding: .ascii))
}) { (error) in
// Login error
result(FlutterError(code: "FAILED", message:"Login failed with exception: " + error.errorDescription, details: nil))
}
}
My dart code:
Future<String> isUserLoggedIn() async {
String status = "";
try {
final String result = await platform
.invokeMethod('loginUser', {"UID": UID, "API_KEY": API_KEY});
print(result); //How to parse?
status = "Hello";
} on PlatformException catch (e) {
print("Exception");
status = e.message;
}
return status;
}
You can pass data in hash map.
In Android:
result.success(hashMapOf(
"CREDITS" to user.credits,
"EMAIL" to user.email,
...
))
In iOS:
let data: [String: Any] = [...]
result(data)
In Flutter:
final result = await platform.invokeMethod<Map<String, dynamic>>('loginUser', ...);
final credits = result['CREDITS'] as String;
final email = result['EMAIL'] as String;
...
you can use invokeMapMethod which is an implementation of invokeMethod that can return typed maps.
like this :
final result = await platform.invokeMapMethod('loginUser', ...);
or you can pass json object as string like that :
in android
platform.success(
"{\"CREDITS\":\"${user.credits}\",\"EMAIL\":\"${user.email}\",\"LAST_ACTIVE\":\"${user.lastActiveAt}\"}"
)
in flutter
var result = await methodChannel.invokeMethod('loginUser' , '');
var json = json.decode(result);
var credit = json['CREDITS'];
var email = json['EMAIL'];
var lastActive = json['LAST_ACTIVE'];
I'm using Firebase notifications on my app and for some reason, the notifications will not open the app on android. I really don't have any clue what is causing this because some apps work fine and others don't.
Does anyone have any idea what would be causing this?
I use this code to send notifications to a specific device. Sometimes they open, other times they dont.
var admin = require("firebase-admin");
var Geopoint = require("geopoint");
var serviceAccount = require("./serviceaccount.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://myapp.firebaseio.com"
});
var db = admin.database();
const title = "My Test";
const body = "My Test Body";
const deepLink = "http://google.com/";
const token = "firebaseDeviceToken";
var payload = {
notification: {
title,
body,
sound: "default",
deepLink
},
data: {
title,
body,
deepLink
}
};
var options = {
priority: "high",
timeToLive: 60 * 60 * 24,
contentAvailable: true
};
console.log(payload);
return admin
.messaging()
.sendToDevice(token, payload, options)
.then(r => {
console.log(JSON.stringify({ r }));
process.exit(0);
})
.catch(err => {
console.log(JSON.stringify({ r: err }));
process.exit(1);
});
I need help, I've been searching for solutions all day but I can't fix my issue, the code below won't read the device tokens.
Below contains my db structure. I manage to receive the log: 'We have a new News for you.' When I added a new post but I received the log "There are no notification tokens to send to." Which means it cannot detect the device tokens even though there is already ones. What am I doing wrong?
{
"Newsv2" : {
"All" : {
"-Ktr7ZkuChCjsUIMb_4f" : {
"title" : "",
"type" : "",
}
},
"Usersv2" : {
"h0RzzpdO7nZVLpAR4fi7xRWUqsT2" : {
"device_token" : "",
"name" : "",
"user_no" : ""
}
},
}
/--News
--All
--name
--desc
/--Usersv2
--{userID}
--device_token
exports.sendNotif = functions.database.ref('/Newsv2/All/{newsID}').onWrite(event => {
const newsID = event.params.newsID;
const userID = event.params.userID;
if (!event.data.val()) {
return console.log('News! ', newsID);
}
console.log('We have a new News for you!',newsID);
// Get the list of device notification tokens.
const getDeviceTokensPromise = admin.database().ref(`/Usersv2/${userid}/device_token`).once('value');
return Promise.all([getDeviceTokensPromise]).then(results => {
const tokensSnapshot = results[0];
//const follower = results[1];
// Check if there are any device tokens.
if (!tokensSnapshot.hasChildren()) {
return console.log('There are no notification tokens to send to.');
}
console.log('There are', tokensSnapshot.numChildren(), 'tokens to send notifications to.');
//console.log('Fetched follower profile', follower);
// Notification details.
const payload = {
notification: {
title: 'Test Message',
body: '',
icon: ''
}
};
// Listing all tokens.
const tokens = Object.keys(tokensSnapshot.val());
// Send notifications to all tokens.
return admin.messaging().sendToDevice(tokens, payload).then(response => {
// For each message check if there was an error.
const tokensToRemove = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
console.error('Failure sending notification to', tokens[index], error);
// Cleanup the tokens who are not registered anymore.
if (error.code === 'messaging/invalid-registration-token' ||
error.code === 'messaging/registration-token-not-registered') {
tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
}
}
});
return Promise.all(tokensToRemove);
});
});
});
To get the device token I store it in my firebase DB when a user registers or logs in.
private DatabaseReference mUserDatabase;
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users/");
//and if the login/register is successful
mUserDatabase.child("device_token").setValue(deviceToken).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Intent intent = new Intent(application.getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_NEW_TASK);
application.startActivity(intent);
}
});
as for my firebase funciton:
const deviceToken = admin.database().ref(`/Users/${unique_id}/device_token`).once('value');
Below code is my firebase function index.js file
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref("Notifications/{userId}")
.onWrite(event => {
var request = event.data.val();
var payload = {
data: {
title: "Welcome to ChitChat Group",
message: "You may have new messages"
}
};
admin.messaging().sendToDevice(request.token, payload)
.then(function (response) {
console.log("Successfully sent message: ", response);
})
.catch(function (error) {
console.log("Error sending message: ", error);
})
});
below code contains where i crate token when user get registered
String uid = (String) firebaseAuth.getCurrentUser().getUid();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Notifications/"+uid);
reference.child("token").setValue(FirebaseInstanceId.getInstance().getToken());
You can make all your users subscribe to one big topic like registration on newMembers then send notification to the topic :
var message = {
to: '/topics/newMembers',
notification: {
title: 'title',
body: 'body'
},
};
guys, I want to send a notification to the android device using npm-gcm
and here is my code:
var sender = new gcm.Sender('myAPIkey');
var message = new gcm.Message();
message.addNotification('title', 'Backend!!!');
message.addNotification('body', 'Bitch');
var regTokens = ['deviceToken1'];
console.log(message)
regTokens.push('deviceToken2')
sender.send(message, { registrationTokens: regTokens }, function (err, response) {
if (err) console.error(err);
else console.log(response);
});
and in the console I get:
{ multicast_id: 7664202372197545000,
success: 2,
failure: 0,
canonical_ids: 0,
results:
[ { message_id: '0:1481660978053751%cc7dad02f9fd7ecd' },
{ message_id: '0:1481660978045814%cc7dad02f9fd7ecd' } ]
}
but the devices did not receive any notification. What is the problem?
Why is this happening? Can you give me an advice?!
Based on this SO post,there might be an issue with "notification" object on node-gcm code. Try to use message.addData and check if it works. Make sure that you followed this tutorial and this thread that gives sample code on how to send simple message to your device.
var sender = new gcm.Sender(googleApiKey);
var message = new gcm.Message();
message.addData('key1','testdarinodegcm');
message.delay_while_idle = 1;
var registrationIds = [];
registrationIds.push('APA91bHCzBP6wEfExCZlSXgf0wuOC6QEWJ-7MVFl3hgaW3Jv8FslsofGJ- qgdliyS8zjwy_W7zPaKsEZx6kbeuWdoAAexawKl1Qd6GEGV_v844n1LMRaTsWeiwI9iaLGKKq_R3scY_wuRG8uG2SZ5X9q0J67Ko3gcw');
sender.send(message, registrationIds, 4, function (err, result) {
console.log(result);
});
Hope this helps!