I've create push notification for my android app, it's work when I try to send it from firebase console. Now what I want is make a push notification when user click on register then show the notification for other users.
I've search in google but didn't find one of the example. The goals is to notif other user that there are new user register in my app.
Thanks for help
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMessagingService";
public static final int ID_SMALL_NOTIFICATION = 235;
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
// TODO(developer): Handle FCM messages here.
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
sendNotification("Hi ini isinya");
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG,"Message Notification Title" + remoteMessage.getNotification().getTitle());
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, ID_SMALL_NOTIFICATION, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = "fcm_default_channel";
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.icon)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(ID_SMALL_NOTIFICATION, notificationBuilder.build());
}
}
I didn't get what you mean but you can use Volly Json object request
First you need to copy your server key from Firebase Console, open up Firebase Console and choose your project
Second add Volley dependency in your project
compile 'com.mcxiaoke.volley:library:1.0.19'
then you can add this code to push
private void sendFCMPush() {
String SERVER_KEY = YOUR_SERVER_KEY;
String msg = "this is test message";
String title = "my title";
String token = FCM_TOKEN;
JSONObject obj = null;
JSONObject objData = null;
JSONObject dataobjData = null;
try {
obj = new JSONObject();
objData = new JSONObject();
objData.put("body", msg);
objData.put("title", title);
objData.put("sound", "default");
objData.put("icon", "icon_name"); // icon_name
objData.put("tag", token);
objData.put("priority", "high");
dataobjData = new JSONObject();
dataobjData.put("text", msg);
dataobjData.put("title", title);
obj.put("to", token);
//obj.put("priority", "high");
obj.put("notification", objData);
obj.put("data", dataobjData);
Log.e("return here>>", obj.toString());
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, Constants.FCM_PUSH_URL, obj,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.e("True", response + "");
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("False", error + "");
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Authorization", "key=" + SERVER_KEY);
params.put("Content-Type", "application/json");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
int socketTimeout = 1000 * 60;// 60 seconds
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
jsObjRequest.setRetryPolicy(policy);
requestQueue.add(jsObjRequest);
}
Hope this help
You want to send a push when one user clicks to register to everyone else who have already registered, right?.
You need to make a HTTP POST request to this address:
https://fcm.googleapis.com/fcm/send
With a header called "Authorization" with value like this "key=AIza...".
So in the body of the request you send a JSON like this.
{
"to": "/topics/foo-bar",
"data": {
"message": "This is a Firebase Cloud Messaging Topic Message!",
}
}
And then you need to create a topic and subscribe the device to the same topic.
FirebaseMessaging.getInstance().subscribeToTopic("foo-bar");
That should work just fine.
Related
I am working on an android chat app using firebase. Everything works fine except push notification. I have googled and read almost every post concerning the issue but still haven't figured out the solution!
Issue: On a fresh installation of the app to the device! I get the notification (ONLY ONCE FIRST TIME) then after that, there is no notification displayed whatsoever. Volley returns a success message "Multicast id remains the same for each success message and even the message-id remains the same for each success message" wondering it has to do with the problem! please have a look at the below code:
1) MY PHP SCRIPT RUNNING ON MY OWN WEB ADDRESS (APP SERVER):
<?php
define( 'API_ACCESS_KEY', 'AAAAeaFcZdA:APA91bFqhLc..............' );
$token = $_GET["token"];
$title = $_POST["title"];
$notification = $_POST["message"];
$msg =
[
'message' => $notification,
'title' => $title,
];
$fields =
[
'to' => $token,
'data' => $msg
];
$headers =
[
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close( $ch );
echo $result;
?>
2) My MessagingService (Firebase) to get Notifications, Since I get the first notification, I believe its configured properly!
public class MessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Timber.i("From: " + remoteMessage.getFrom());
//Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Timber.i("Message data payload: " + remoteMessage.getData());
Map<String, String> data = remoteMessage.getData();
sendNotification(data);
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Timber.i("Message Notification Body: " + remoteMessage.getNotification().getBody());
Map<String, String> data = remoteMessage.getData();
sendNotification(data);
}
Map<String, String> data = remoteMessage.getData();
sendNotification(data);
}
#Override
public void onNewToken(#NonNull String token) {
Timber.i("MyToken: " + token);
sendRegistrationToServer(token);
}
private void sendRegistrationToServer(String token) {
// TODO: Implement this method to send token to your app server.
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference usersdRef = database.getReference("users");
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
usersdRef.child(user.getUid()).child("tokens").setValue(token);
}
}
private void sendNotification(Map<String, String> messageBody) {
String myTitle = messageBody.get("title");
final int not_nu = generateRandom();
Intent intent = new Intent(this, ChatMessageActivity.class);
intent.putExtra("username", myTitle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
ChatUserModel.chatWith = myTitle;
PendingIntent pendingIntent = PendingIntent.getActivity(this, not_nu /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.marker)
.setContentTitle(messageBody.get("title"))
.setContentText(messageBody.get("message"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(not_nu /* ID of notification */, notificationBuilder.build());
}
public int generateRandom() {
Random random = new Random();
return random.nextInt(9999 - 1000) + 1000;
}}
3) My Chat Activity where I am sending the message to another user.
getOtherToken = other user token id to whom I will send the message
sendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String messageText = messageArea.getText().toString();
SimpleDateFormat sdf = new SimpleDateFormat("dd.MMM.yyyy", Locale.getDefault());
String currentDate = sdf.format(new Date());
String currentTime = new SimpleDateFormat("HH:mm", Locale.getDefault()).format(new Date());
if (!messageText.equals("")) {
Map<String, String> map = new HashMap<String, String>();
map.put("message", messageText);
map.put("user", user.getDisplayName());
map.put("timetoshow", currentTime);
map.put("dateStamp", currentDate);
usersmRef.child(reference1).push().setValue(map);
usersmRef.child(reference2).push().setValue(map);
messageArea.setText("");
sendNotificationToappServer(getOtherToken, user.getDisplayName(), messageText);
Timber.i("GetOtherTOken: " + user.getDisplayName() + " | " + getOtherToken + " | " + messageText);
}
}
});
4) My Volley Method in the Same ChatActivity.java as above where I send String Request to my App Server, the token here is of the user who will receive the message!
public void sendNotificationToappServer(String token, String title, String notification) {
String SERVER_ADDRESS = "https://www.mywebsite/notification.php?token=";
RequestQueue requestQueue;
// Instantiate the cache
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap
// Set up the network to use HttpURLConnection as the HTTP client.
Network network = new BasicNetwork(new HurlStack());
// Instantiate the RequestQueue with the cache and network.
requestQueue = new RequestQueue(cache, network);
// Start the queue
requestQueue.start();
StringRequest stringRequest = new StringRequest(Request.Method.POST, SERVER_ADDRESS + token,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(ChatMessageActivity.this, response, Toast.LENGTH_LONG).show();
Timber.i("Volley: %s", response.toString());
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(ChatMessageActivity.this, error.toString(), Toast.LENGTH_LONG).show();
Timber.i("Volley Error: " + error.toString());
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("title", title);
params.put("message", notification);
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};
requestQueue.add(stringRequest);
}
5) My Volley Response recorded in Android Studio which is a success, For the first time, it delivers push notification but not after that until fresh install!
Anything if required other than above! please let me know. Fingers crossed!
I was able to fix the issue! Well, I found out the problem was cache management due to volley. Before posting a new queue request, clearing the cache will fix the issue!
queue.getCache().clear();
Also, I preferred using singleton class for Volley. Here is the updated code for sendNotificationToappServer method above:
public void sendNotificationToappServer(String token, String title, String notification) {
String SERVER_ADDRESS = "https://www.mywebsite/notification.php?token=";
// Get a RequestQueue
RequestQueue queue = MySingleton.getInstance(ChatMessageActivity.this).
getRequestQueue();
StringRequest stringRequest = new StringRequest(Request.Method.POST, SERVER_ADDRESS + token,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(ChatMessageActivity.this, response, Toast.LENGTH_LONG).show();
Timber.i("Volley: %s", response.toString());
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(ChatMessageActivity.this, error.toString(), Toast.LENGTH_LONG).show();
Timber.i("Volley Error: " + error.toString());
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("title", title);
params.put("message", notification);
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};
queue.getCache().clear();
MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);
}
The PHP code in the question above works perfectly! so if you are looking for PHP script for firebase Cloud messaging App server for one on one messaging, go for it!
I am always getting an error on sending notification from android device in logcat I have tried so many times but nothing getting:
{"multicast_id":5162718122421221171,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
Here is my android code for sending message I am sending userABC topic in the topic section.
public void SendNotification(String Title, String Message){
NOTIFICATION_TITLE = Title;
NOTIFICATION_MESSAGE = Message;
JSONObject notification = new JSONObject();
JSONObject notifcationBody = new JSONObject();
try {
notifcationBody.put("title", NOTIFICATION_TITLE);
notifcationBody.put("message", NOTIFICATION_MESSAGE);
notification.put("to", TOPIC);
notification.put("data", notifcationBody);
} catch (JSONException e) {
Log.e(TAG, "onCreate: " + e.getMessage() );
}
sendNotification(notification);
}
private void sendNotification(JSONObject notification) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(FCM_API, notification,
new com.android.volley.Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.i(TAG, "onResponse: " + response.toString());
}
},
new com.android.volley.Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), "Request error", Toast.LENGTH_LONG).show();
Log.i(TAG, "onErrorResponse: Didn't work");
}
}){
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Authorization", serverKey);
params.put("Content-Type", contentType);
return params;
}
};
MySingleton.getInstance(getActivity().getApplicationContext()).addToRequestQueue(jsonObjectRequest);
}
Here is my FirebaseInstanceIDService.java class
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "mFirebaseIIDService";
private static final String SUBSCRIBE_TO = "userABC";
#Override
public void onTokenRefresh() {
/*
This method is invoked whenever the token refreshes
OPTIONAL: If you want to send messages to this application instance
or manage this apps subscriptions on the server side,
you can send this token to your server.
*/
String token = FirebaseInstanceId.getInstance().getToken();
// Once the token is generated, subscribe to topic with the userId
FirebaseMessaging.getInstance().subscribeToTopic(SUBSCRIBE_TO);
Log.i(TAG, "onTokenRefresh completed with token: " + token);
sendRegistrationToServer(token);
}
private void sendRegistrationToServer(String token) {
// send token to web service ??
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("server/saving-data/IDs");
// then store your token ID
ref.push().setValue(token);
}
}
Here is my FirebaseMessagingService.java class
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private final String ADMIN_CHANNEL_ID ="admin_channel";
#Override
public void onNewToken(String token) {
Log.d("TAG", "Refreshed token: " + token);
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d("THIS", "Refreshed token: " + refreshedToken);
FirebaseMessaging.getInstance().subscribeToTopic("userABC");
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
// send token to web service ??
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("server/saving-data/IDs");
// then store your token ID
ref.push().setValue(token);
}
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
final Intent intent = new Intent(this, MainActivity.class);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int notificationID = new Random().nextInt(3000);
/*
Apps targeting SDK 26 or above (Android O) must implement notification channels and add its notifications
to at least one of them. Therefore, confirm if version is Oreo or higher, then setup notification channel
*/
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
setupChannels(notificationManager);
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this , 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher);
Uri notificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, ADMIN_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(largeIcon)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("message"))
.setAutoCancel(true)
.setSound(notificationSoundUri)
.setContentIntent(pendingIntent);
//Set notification color to match your app color template
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
notificationBuilder.setColor(getResources().getColor(R.color.colorPrimaryDark));
}
notificationManager.notify(notificationID, notificationBuilder.build());
}
#RequiresApi(api = Build.VERSION_CODES.O)
private void setupChannels(NotificationManager notificationManager){
CharSequence adminChannelName = "New notification";
String adminChannelDescription = "Device to devie notification";
NotificationChannel adminChannel;
adminChannel = new NotificationChannel(ADMIN_CHANNEL_ID, adminChannelName, NotificationManager.IMPORTANCE_HIGH);
adminChannel.setDescription(adminChannelDescription);
adminChannel.enableLights(true);
adminChannel.setLightColor(Color.RED);
adminChannel.enableVibration(true);
if (notificationManager != null) {
notificationManager.createNotificationChannel(adminChannel);
}
}
}
FirebaseInstanceIdService is deprecated now.
So you can change your registration logic as below
FcmRegistrationManager.java
public class FcmRegistrationManager {
private static final String TAG = "FcmRegistrationManager"
/*This is async call*/
public void registerWithFcm(){
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
#Override
public void onComplete(#NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "getInstanceId failed", task.getException());
return;
}
// Get new Instance ID token
String token = task.getResult().getToken();
Log.d(TAG, msg);
sendRegistrationToServer(token)
}
});
}
private void sendRegistrationToServer(String token) {
// send token to web service ??
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("server/saving-data/IDs");
// then store your token ID
ref.push().setValue(token);
}
}
No change for sendNotification() and FirebaseMessagingService class.
More reference : https://firebase.google.com/docs/cloud-messaging/android/client
Let me still not working, I will share you working code.
I have a list of Firebase message objects, each of which may be updated before an activity is opened, at which point a Firebase ChildEventListener() fires off (children are handled in onChildAdded()).
The update uses a setValue() on the status field:
database.getReference("Messages").child(roomID).child(msgID).child("status").setValue("delivered");
The ChildEventListener() fires off fine for all children when the activity is opened. However, for those children (messages) that had their status' updated before hand, they return null for all fields except the status field.
In the Firebase database, all fields are as they should be (non-null), so there is nothing wrong with the data.
Upon exiting the activity and re-entering, all children are returned in the ChildEventListener() as they should be (no more null data returned).
In short, two ChildEventListener() calls are required to return all the data for a child after a setValue() is run on that child.
More code and context can be provided if required. Let me know.
Edit 1: here is the listener: (it is in onResume())
newMessageListner = myMessageRoomRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, String previousChildName) {
if (dataSnapshot.exists()) {
String messageKey = dataSnapshot.getKey();
Message messageClass = dataSnapshot.getValue(Message.class);
String messageUserID = messageClass.getUserId();
Log.d(TAG, "Message Key: " + messageKey);
Log.d(TAG, "Message Status: " + messageClass.getStatus());
Log.d(TAG, "Message User ID: " + messageUserID);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Edit 2: I call the getValue() from sendNotification() in my FirebaseMessagingService
private void sendNotification(RemoteMessage remoteMessage) {
//Intent intent = new Intent(this, StudentChatActivity.class);
String clickAction = remoteMessage.getData().get("click_action");
Intent intent = new Intent(clickAction);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
String roomID = remoteMessage.getData().get("ROOMID");
String origin = remoteMessage.getData().get("ORIGIN");
String msgID = remoteMessage.getData().get("MSGID");
Log.d(TAG, "Message data payload, roomID: " + roomID);
database.getReference("Messages").child(roomID).child(msgID).child("status").setValue("delivered");
intent.putExtra("ROOMID", roomID);
intent.putExtra("USERID", UserID);
intent.putExtra("USERNAME", UserName);
intent.putExtra("ORIGIN", origin);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.received_message);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.small_pickle)
.setContentTitle("FCM Message")
.setContentText(remoteMessage.getData().get("body"))
.setPriority(1)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
I have made a one to one chat app and I have implemented chat notifications but when I am sending notifications from the user app to the doctor app but my doctor app crashes as soon as I am clicking on the chat notification
This is my Java class where I am handling the notification if my app is in foreground so this class will handle notifications so now in this class I am calling firebase database and taking some data from there and passing that data as an extra data with the resulting intent but when that resulting intent directs the app to the particular chat view that database data is not there
This is the Log of my app
E/MyFirebaseMessaging: From:408315985482
07-05 16:19:33.803 4399-6278/com.tech.pritz.bemoprovider D/MyFirebaseMessaging: in between Firebase and auth
07-05 16:19:33.805 4399-6278/com.tech.pritz.bemoprovider D/MyFirebaseMessaging: Message data payload: {idRe, username=komal , message=hi}
07-05 16:19:33.805 4399-6278/com.tech.pritz.bemoprovider D/MyFirebaseMessaging: app in background
07-05 16:19:33.813 4399-6278/com.tech.pritz.bemoprovider E/MyFirebaseMessaging: image not found
07-05 16:19:33.825 4399-6278/com.tech.pritz.bemoprovider D/NotificationUtil: New Message
D/MyFirebaseMessaging: Entered into the database
07-05 16:19:34.359 4399-4399/com.tech.pritz.bemoprovider D/MyFirebaseMessaging: HKNi1sezUpbwJSeEGos44FtfUw43
You can see here it is showing entered in the database also and it is getting that data also but when I pass it as an intent it is not found in another activity.
Crash log
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'java.lang.String
com.tech.pritz.bemoprovider.model.Provider.getId()' on a null object
reference at
com.tech.pritz.bemoprovider.bemo.ChatView.ini(ChatView.java:350) at
com.tech.pritz.bemoprovider.bemo.ChatView.onCreate(ChatView.java:150)
public class MyFirebaseMessaging extends FirebaseMessagingService
{
private static final String TAG=MyFirebaseMessaging.class.getSimpleName();
private NotificationUtil notificationUtil;
private DatabaseReference pathFirebase;
private FirebaseAuth auth;
private Provider mprovider;
#Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
Log.e(TAG,"From:"+ remoteMessage.getFrom());
pathFirebase = FirebaseDatabase.getInstance().getReference();
Log.d(TAG,"in between Firebase and auth");
auth = FirebaseAuth.getInstance();
if(remoteMessage ==null )
{
return;
}
//check if message contains a notification payload
if(remoteMessage.getNotification()!=null)
{
String tittle = remoteMessage.getNotification().getTitle();
Log.e(TAG,"Notification Body:" + remoteMessage.getNotification().getBody());
handleNotification(remoteMessage.getNotification().getBody(),tittle,null, mprovider);
}
//checks to see if message contains a data payload
if(remoteMessage.getData().size() > 0)
{
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
String imageUrl = remoteMessage.getData().get("image");
// String message = remoteMessage.getData().get("message");
// String title = remoteMessage.getData().get("title");
// String name = remoteMessage.getData().get("username");
String message= remoteMessage.getData().get("message");
String name= remoteMessage.getData().get("username");
String title= "New Message";
String iduser= remoteMessage.getData().get("iduser");
String email= remoteMessage.getData().get("email");
String gcm_id= remoteMessage.getData().get("idGCM");
String conversation_id=remoteMessage.getData().get("conversation_id");
Log.d(TAG,"app in background");
pathFirebase.child("users").child(auth.getCurrentUser().getUid())
.addValueEventListener(
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Log.d(TAG,"Entered into the database");
mprovider = dataSnapshot.getValue(Provider.class);
UserDataHolder.getInstance().setmName(mprovider.getName());
// Intent intent = new Intent(MyFirebaseMessaging.this, ListPatients.class);
mprovider.setId(auth.getCurrentUser().getUid());
UserDataHolder.getInstance().setmUserId(mprovider.getId());
// intent.putExtra("user", provider);
//startActivity(intent);
//finish();
Log.d(TAG,mprovider.getId());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(MyFirebaseMessaging.this, "Failed", Toast.LENGTH_SHORT).show();
}
});
// Log.d(TAG,mprovider.getId());
// app is in background, show the notification in notification tray
Intent resultIntent = new Intent(getApplicationContext(),ChatView.class);
resultIntent.putExtra("user_id", iduser);
resultIntent.putExtra("email", email);
resultIntent.putExtra("User_display_name",name);
resultIntent.putExtra("user_gcm_id",gcm_id);
resultIntent.putExtra("conversation_id",conversation_id);
// resultIntent.putExtra("providerLocal",mprovider);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// resultIntent.putExtra("message", message);
// check for image attachment
if (TextUtils.isEmpty(imageUrl))
{
Log.e(TAG,"image not found");
showNotificationMessage(getApplicationContext(), title, message,name,resultIntent,mprovider);
}
else
{
// image is present, show notification with image
Log.e(TAG,"image found");
showNotificationMessageWithBigImage(getApplicationContext(), title, message, resultIntent, imageUrl);
}
}
}
private void handleNotification(String message,String title,String name,Provider provider)
{
if(!NotificationUtil.isAppInBackground(getApplicationContext()))
{
Intent resultIntent = new Intent(getApplicationContext(), ChatView.class);
showNotificationMessage(getApplicationContext(), title, message,null,resultIntent, provider);
}
else
{
Intent resultIntent = new Intent(getApplicationContext(), ChatView.class);
showNotificationMessage(getApplicationContext(), title, message,null,resultIntent, mprovider);
}
}
private void showNotificationMessageWithBigImage(Context applicationContext, String title, String message, Intent resultIntent, String imageUrl)
{
notificationUtil = new NotificationUtil(applicationContext);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtil.showNotificationMessage(title, message,null,resultIntent,imageUrl);
}
private void showNotificationMessage(Context applicationContext, String title, String message,String name, Intent resultIntent,Provider mprovider)
{
// Log.d(TAG,mprovider.getId());
notificationUtil = new NotificationUtil(applicationContext);
resultIntent.putExtra("providerLocal",mprovider);
// resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtil.showNotificationMessage(title, message,name,resultIntent);
}
}
the problem is in this line :
UserDataHolder.getInstance().setmUserId(mprovider.getId());
the mprovider object is null
I'm working on an app that has firebase notifications, i followed through the firebase tutorial guides in order to how to send and receive notifications but I'm receiving 2 notifications on other devices instead of receiving 1 and receiving a notification from the sent device although that device shouldn't receive the notification that was sent from it
I saw other questions regarding uploading notifications twice but it didn't work for me
P.S: the notification is sent from a custom dialog that has title edit text and body edit text
here is the manifest:
<service
android:name=".MyFirebaseInstanceIDService"
android:exported="false">
<intent-filter>
<action
android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action
android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
I'm compiling these in the gradle:
compile 'com.google.firebase:firebase-core:10.0.0'
compile 'com.google.firebase:firebase-messaging:10.0.0'
Here's the code for the custom dialog:
builder = new AlertDialog.Builder(this);
final View view = View.inflate(this, R.layout.layout_send_notification, null);
builder.setView(view)
.setCancelable(true)
.setPositiveButton(getString(R.string.send), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
EditText editMessage = (EditText) view.findViewById(R.id.notification_message);
EditText editTitle = (EditText) view.findViewById(R.id.notification_title);
Calendar now = Calendar.getInstance();
int day = now.get(Calendar.DAY_OF_MONTH);
int month = now.get(Calendar.MONTH) + 1;
int year = now.get(Calendar.YEAR);
String message = editMessage.getText().toString().replace(' ', '_');
String title = editTitle.getText().toString().replace(' ', '_');
boolean cancel = false;
View focusView = null;
if (TextUtils.isEmpty(message)) {
editMessage.setError(getString(R.string.error_field_required));
focusView = editMessage;
cancel = true;
}
if (TextUtils.isEmpty(title)) {
editTitle.setError(getString(R.string.error_field_required));
focusView = editTitle;
cancel = true;
}
if (cancel)
focusView.requestFocus();
else {
RequestQueue queue = Volley.newRequestQueue(NotificationsActivity.this);
String url = "https://mysite.here/send_push.php?" +
"message=" + message +
"&title=" + title +
"&day=" + day +
"&month=" + month +
"&year=" + year;
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("send push response", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
queue.add(request);
}
}
})
.setNegativeButton(getString(R.string.button_cancel), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
sendNotificationDialog = builder.create();
here is the firebaseMessagingService:
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
private void sendNotification(String messageTitle, String messageBody) {
Intent intent = new Intent(this, SplashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setSmallIcon(R.drawable.ic_stat_spe_bau)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 , notificationBuilder.build());
}
here is the firebaseInstanceIDService:
#Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d("firebase ID", refreshedToken);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
String url = "https://mysite.here/insertFCM_ID.php?fcmID=" + token;
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("fcm response", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
queue.add(request);
}
send_push.php code:
<?php
header("Content-type: application/json, charset=UTF-8");
require('db.php');
$db = mysqli_connect($host, $username, $password) or die('Could not connect');
mysqli_select_db($db, $db_name) or die('');
$message = $_GET['message'];
$title = $_GET['title'];
$day = $_GET['day'];
$month = $_GET['month'];
$year = $_GET['year'];
$server_key = "somekey";
$sql = 'select fcm_token from fcm_info';
$result = mysqli_query($db, $sql) or print("Error");
$key = array();
while ($row = mysqli_fetch_assoc($result)) {
$key[] = $row;
}
$headers = array(
'Authorization: key=' . $server_key,
'Content-Type: application/json'
);
$single = "";
foreach($key as $value) {
$single = $value['fcm_token'];
$fields = array(
'to' => $single,
'notification' => array(
'title' => str_replace("_", " ", $title),
'body' => str_replace("_", " ", $message),
'vibrate' => 1,
'sound' => 1
)
);
$payload = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$result = curl_exec($ch);
}
mysqli_query($db, "INSERT INTO notifications (title, body, day, month, year) VALUES ('$title', '$message', '$day', '$month', '$year')");
mysqli_close($db);
echo $result;
?>
i tried a lot of ways in order to find where the problem is but i couldn't know what it is
if more information is needed i will re-edit this to add those required information
Make comment line in your onMessageReceived event:
//sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
Try again after you make this change in your firebaseInstanceIDService class. It should be ok.
If you want to use only Downstream messages, you need to decide your message type. Data messages or notifications. You also need to manage 3 situations of your app on the device: Foreground, Background and Killed. Data messages are always trigger onMessageReceived event on your Service. This is different when you send notification message. I suggest you sending Data messages as Post requests. You don't have any problem now. You are sending Post request to 'https://fcm.googleapis.com/fcm/send' URL via Volley and you are sending a notification. Then your service is receiving your notification and create another one. It is very normal to receive 2 notifications, because your code is generating 2 notification. I told you make comment line and test it.
You can save the message-id in a variable then when a new message received twice, the first message-id will be saved in this variable and when it called twice the second time you will check if exists in your variable or not .. for example
String savedMessageId = "";
public void setupFCM() {
if (savedMessageId != remoteMessage.messageId)
savedMessageId = remoteMessage.messageId;
else
return;
// your code ...
}