android-firebase-chat icon indicating copy to clipboard operation
android-firebase-chat copied to clipboard

Notification

Open saleham5 opened this issue 6 years ago • 33 comments

is there anybody solve pushNotification problem??

saleham5 avatar Dec 10 '17 14:12 saleham5

yes

R33N4K0R avatar Dec 11 '17 05:12 R33N4K0R

@MintuReena Could you please let me know how to do it? How to send notification to user when he/she receives a new message without the need to check the chats for new messages in the application? Thanks guys

ahmaddaas94 avatar Feb 04 '18 19:02 ahmaddaas94

give me your mail address ill send u the code of notification

R33N4

On Mon, Feb 5, 2018 at 1:29 AM, ahmaddaas94 [email protected] wrote:

@MintuReena https://github.com/mintureena Could you please let me know how to do it? How to send notification to user when he/she receives a new message without the need to check the chats for new messages in the application? Thanks guys

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/hieuapp/android-firebase-chat/issues/30#issuecomment-362934720, or mute the thread https://github.com/notifications/unsubscribe-auth/AYpEynwNptrEvjjVQ0Ua3MrCT4eIWC5Qks5tRgwxgaJpZM4Q8git .

R33N4K0R avatar Feb 05 '18 06:02 R33N4K0R

Hello

Can i have the code for the notification? I have try the code, but i just can receive one time for the message notification! Could you help ?

Regards Riff

On Mon, 5 Feb 2018 at 14:01, ReenaKoR [email protected] wrote:

give me your mail address ill send u the code of notification

R33N4

On Mon, Feb 5, 2018 at 1:29 AM, ahmaddaas94 [email protected] wrote:

@MintuReena https://github.com/mintureena Could you please let me know how to do it? How to send notification to user when he/she receives a new message without the need to check the chats for new messages in the application? Thanks guys

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub < https://github.com/hieuapp/android-firebase-chat/issues/30#issuecomment-362934720 , or mute the thread < https://github.com/notifications/unsubscribe-auth/AYpEynwNptrEvjjVQ0Ua3MrCT4eIWC5Qks5tRgwxgaJpZM4Q8git

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/hieuapp/android-firebase-chat/issues/30#issuecomment-362989492, or mute the thread https://github.com/notifications/unsubscribe-auth/Ac8UWfEgK0nrDo-HxEPWPbN2rTjRVvFZks5tRpkkgaJpZM4Q8git .

RiffChan avatar Feb 05 '18 14:02 RiffChan

@MintuReena Thanks for the reply, I really appreciate your help. Here is my email: [email protected] Thanks alot Ahmad

ahmaddaas94 avatar Feb 05 '18 16:02 ahmaddaas94

@RiffChan give me your email address

R33N4K0R avatar Feb 06 '18 06:02 R33N4K0R

public class FcmNotificationBuilder { public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8"); private static final String TAG = "FcmNotificationBuilder"; private static final String SERVER_API_KEY = "Your Server Api Key"; private static final String CONTENT_TYPE = "Content-Type"; private static final String APPLICATION_JSON = "application/json"; private static final String AUTHORIZATION = "Authorization"; private static final String AUTH_KEY = "key=" + SERVER_API_KEY; private static final String FCM_URL = "https://fcm.googleapis.com/fcm/send"; // json related keys private static final String KEY_TO = "to"; private static final String KEY_NOTIFICATION = "notification"; private static final String KEY_TITLE = "title"; private static final String KEY_TEXT = "text"; private static final String KEY_DATA = "data"; private static final String KEY_USERNAME = "username"; private static final String KEY_UID = "uid"; private static final String KEY_ROOMID = "roomId"; private static final String KEY_FCM_TOKEN = "fcm_token"; private static final String KEY_FCM_GROUPID = "GroupId"; private static final String KEY_BITMAP = "bitmap"; private String mTitle; private String mMessage; private String mUsername; private String mUid; private String mroomId; private String mFirebaseToken; private String mGroupId; private String mReceiverFirebaseToken; private String mBitmap; private FcmNotificationBuilder() { } public static FcmNotificationBuilder initialize() { return new FcmNotificationBuilder(); } public FcmNotificationBuilder title(String title) { mTitle = title; return this; } public FcmNotificationBuilder message(String message) { mMessage = message; return this; } public FcmNotificationBuilder username(String username) { mUsername = username; return this; } public FcmNotificationBuilder uid(String uid) { mUid = uid; return this; } public FcmNotificationBuilder roomId(String roomId) { mroomId = roomId; return this; } public FcmNotificationBuilder firebaseToken(String firebaseToken) { mFirebaseToken = firebaseToken; return this; } public FcmNotificationBuilder GroupId(String GroupId) { mGroupId = GroupId; return this; } public FcmNotificationBuilder receiverFirebaseToken(String receiverFirebaseToken) { mReceiverFirebaseToken = receiverFirebaseToken; return this; } public FcmNotificationBuilder bitmap(String bitmap) { mBitmap = bitmap; return this; } public void send() { RequestBody requestBody = null; try { requestBody = RequestBody.create(MEDIA_TYPE_JSON, getValidJsonBody().toString()); } catch (JSONException e) { e.printStackTrace(); } Request request = new Request.Builder() .addHeader(CONTENT_TYPE, APPLICATION_JSON) .addHeader(AUTHORIZATION, AUTH_KEY) .url(FCM_URL) .post(requestBody) .build();

    Call call = new OkHttpClient().newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            Log.e(TAG, "onGetAllUsersFailure: " + e.getMessage());
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            Log.e(TAG, "onResponse: " + response.body().string());
        }
    });
}
private JSONObject getValidJsonBody() throws JSONException {
    JSONObject jsonObjectBody = new JSONObject();
    jsonObjectBody.put(KEY_TO, mReceiverFirebaseToken);
    JSONObject jsonObjectData = new JSONObject();
    jsonObjectData.put(KEY_TITLE, mTitle);
    jsonObjectData.put(KEY_TEXT, mMessage);
    jsonObjectData.put(KEY_USERNAME, mUsername);
    jsonObjectData.put(KEY_UID, mUid);
    jsonObjectData.put(KEY_ROOMID, mroomId);
    jsonObjectData.put(KEY_FCM_TOKEN, mFirebaseToken);
    jsonObjectData.put(KEY_BITMAP, mBitmap);
    jsonObjectBody.put(KEY_DATA, jsonObjectData);
    return jsonObjectBody;
}

}

R33N4K0R avatar Feb 06 '18 11:02 R33N4K0R

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { private static final String TAG = "MyFirebaseIIDService"; /** * Called if InstanceID token is updated. This may occur if the security of * the previous token had been compromised. Note that this is called when the InstanceID token * is initially generated so this is where you would retrieve the token. */ // [START refresh_token] @Override public void onTokenRefresh() { // Get updated InstanceID token. String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG, "Refreshed token: " + refreshedToken); // If you want to send messages to this application instance or // manage this apps subscriptions on the server side, send the // Instance ID token to your app server. sendRegistrationToServer(refreshedToken); } // [END refresh_token]

/**
 * Persist token to third-party servers.
 * <p>
 * Modify this method to associate the user's FCM InstanceID token with any server-side account
 * maintained by your application.
 *
 * @param token The new token.
 */
private void sendRegistrationToServer(final String token) {
  //  new SharedPrefUtil(getApplicationContext()).saveString(Constants.ARG_FIREBASE_TOKEN, token);

    if (FirebaseAuth.getInstance().getCurrentUser() != null) {
        FirebaseDatabase.getInstance()
                .getReference()
               // .child(Constants.ARG_USERS)
                .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
               // .child(Constants.ARG_FIREBASE_TOKEN)
                .setValue(token);
    }
}

}

R33N4K0R avatar Feb 06 '18 11:02 R33N4K0R

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

/**
 * Called when message is received.
 *
 * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
 */
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    // TODO(developer): Handle FCM messages here.
    // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
    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());

        String title = remoteMessage.getData().get("title");
        String message = remoteMessage.getData().get("text");
        String username = remoteMessage.getData().get("username");
        String uid = remoteMessage.getData().get("uid");
        String fcmToken = remoteMessage.getData().get("fcm_token");
        String GroupId=remoteMessage.getData().get("GroupId");
        String roomId=remoteMessage.getData().get("roomId");
        // Don't show notification if chat activity is open.
        if (!FirebaseChatMainApp.isChatActivityOpen()) {
            sendNotification(title,
                    message,
                    username,
                    uid,
                    fcmToken,
                    GroupId,
                    roomId);
        } else {
            EventBus.getDefault().post(new PushNotificationEvent(title,
                    message,
                    username,
                    uid,
                    fcmToken,
                    roomId));
        }
    }
}

/**
 * Create and show a simple notification containing the received FCM message.
 */
private void sendNotification(String title,
                              String message,
                              String receiver,
                              String receiverUid,
                              String firebaseToken,
                              String GroupId,
                              String roomId) {
    Intent intent = new Intent(this, ChatActivity.class);
    intent.putExtra("ARG_RECEIVER", receiver);
    intent.putExtra("Fname",title);
    intent.putExtra("ARG_RECEIVER_UID", receiverUid);
    intent.putExtra("ARG_FIREBASE_TOKEN", firebaseToken);
    intent.putExtra("GroupId",GroupId);
    intent.putExtra("roomId",roomId);
    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)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setLights(0,128,0)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());
}

}

R33N4K0R avatar Feb 06 '18 11:02 R33N4K0R

R33N4K0R avatar Feb 06 '18 11:02 R33N4K0R

@MintuReena Thanks for the reply, I have added the FCM related classes. However, it worked on Android 21 (Lollipop) but didn't work or newer versions like Nougat. Do you have any idea why this happens? Thanks

ahmaddaas94 avatar Feb 06 '18 20:02 ahmaddaas94

@MintuReena Can you plz send me the all modified code of it

My Email :- [email protected]

naveenrao2k avatar Feb 19 '18 11:02 naveenrao2k

Me too I need the modified files only please @MeetuReena @naveenrao2k Email: [email protected]

darkIncorporative avatar Mar 02 '18 04:03 darkIncorporative

@MeetuReena please reply

darkIncorporative avatar Mar 02 '18 04:03 darkIncorporative

I mean @MintuReena

darkIncorporative avatar Mar 02 '18 04:03 darkIncorporative

@ahmaddaas94 please send me your modified files for the notification I too working for lollipop

darkIncorporative avatar Mar 03 '18 04:03 darkIncorporative

@mintureena where is pushnotificationevent.class code you mentioned it in the last code

darkIncorporative avatar Mar 03 '18 04:03 darkIncorporative

give me your email address I'll send code

R33N4

On Sat, Mar 3, 2018 at 10:09 AM, darkIncorporative <[email protected]

wrote:

@MintuReena https://github.com/mintureena where is pushnotificationevent.class code you mentioned it in the last code

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/hieuapp/android-firebase-chat/issues/30#issuecomment-370117612, or mute the thread https://github.com/notifications/unsubscribe-auth/AYpEykY3FBPcJH3UEPPIiU84GR4PQFVwks5tah6AgaJpZM4Q8git .

R33N4K0R avatar Mar 03 '18 05:03 R33N4K0R

Already gave it above

darkIncorporative avatar Mar 03 '18 05:03 darkIncorporative

@mintuReena I gave it above

darkIncorporative avatar Mar 03 '18 05:03 darkIncorporative

@MintuReena Thanks brother i got it.

naveenrao2k avatar Mar 03 '18 09:03 naveenrao2k

@MintuReena Could you give me the code? Here is my email [email protected]

giovankabisano avatar Aug 02 '18 02:08 giovankabisano

yes sent R33N4

On Thu, Aug 2, 2018 at 7:56 AM Giovanka Bisano Putra < [email protected]> wrote:

@MintuReena https://github.com/MintuReena Cloud you give me the code? Here is my email [email protected]

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/hieuapp/android-firebase-chat/issues/30#issuecomment-409784997, or mute the thread https://github.com/notifications/unsubscribe-auth/AYpEyjUvbxj0cC_ZsuNd1DOs7OKfG4M9ks5uMmM5gaJpZM4Q8git .

R33N4K0R avatar Aug 02 '18 06:08 R33N4K0R

Can u plz send me the modified code I am having the issue with the database when i clicked on add user or login in app get crashed.. mail id: [email protected]

mayurs44 avatar Aug 23 '18 05:08 mayurs44

Can you sende me the code please too? mail: [email protected]

jpsanmiguel avatar Aug 24 '18 18:08 jpsanmiguel

@MintuReena Can you send me code too mail: [email protected]

I just downloaded the app today from GitHub and having so many problems I just staring learning so its hell lot of hard...… errors so im reading all the issues one by one lol xD

blazingdevil44 avatar Sep 07 '18 11:09 blazingdevil44

@MintuReena can u check and see why notification are not working if possible plz I just sent u a mail sorry in advance if its a stupid mistake since I'm new at coding if possible can u check mail: [email protected]

blazingdevil44 avatar Sep 24 '18 08:09 blazingdevil44

if someone can send me the source code of notification push 👍 : [email protected]

RmpNathan avatar Oct 09 '18 16:10 RmpNathan

@MintuReena can u give me the source code of push notification 👍: [email protected]

rumekso23 avatar Mar 09 '19 22:03 rumekso23