Java tutorial
/** * Copyright Google Inc. All Rights Reserved. * <p> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.vedant.hereami.chatfolder; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.media.RingtoneManager; import android.net.Uri; import android.provider.BaseColumns; import android.provider.ContactsContract; import android.support.v4.app.NotificationCompat; import android.util.Log; import com.firebase.client.Firebase; import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; import com.vedant.hereami.MainActivity; import com.vedant.hereami.R; import org.json.JSONException; import org.json.JSONObject; import java.util.HashMap; public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = "MyFirebaseMsgService"; public HashMap<String, String> hashMap = new HashMap<>(); private String contactmatch; private String tendigitnumber; private Bitmap image; private byte[] imageAsBytes; private String connectionstatus2; private String sunny; private Firebase mFirebaseMessagesChatconnectioncheck = new Firebase( "https://iamhere-29f2b.firebaseio.com/users"); private Bitmap _bitmapScaled; private String title; private String title1; @Override public void onMessageReceived(RemoteMessage remoteMessage) { //Displaying data in log //It is optional Log.d(TAG, "From: " + remoteMessage.getFrom()); // Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); if (remoteMessage.getData().size() > 0) { Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString()); try { JSONObject json = new JSONObject(remoteMessage.getData().toString()); sendPushNotification(json); } catch (Exception e) { Log.e(TAG, "Exception: " + e.getMessage()); } } //Calling method to generate notification // sendNotification(remoteMessage.getNotification().getBody()); } //This method is only generating push notification //It is same as we did in earlier posts private void sendNotification(String messageBody) { Intent intent = new Intent(this, MainActivity.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) .setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Firebase Push Notification") .setContentText(messageBody).setAutoCancel(true).setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notificationBuilder.build()); } //this method will display the notification //We are passing the JSONObject that is received from //firebase cloud messaging private void sendPushNotification(JSONObject json) { //optionally we can display the json into log Log.e(TAG, "Notification JSON " + json.toString()); try { //getting the json data JSONObject data = json.getJSONObject("data"); //parsing json data title1 = data.getString("title"); String[] parts = title1.replace("+", ":").split(":"); // escape . String part1 = parts[0]; String part2 = parts[1]; // String tendigitnumber = getLastThree(part2); sunny = part1.replace("dot", "."); tendigitnumber = getLastThree(part2); contactmatch = getContactDisplayNameByNumber(tendigitnumber); title = contactmatch; //getdp(); String message = data.getString("message"); String imageUrl = data.getString("image"); //creating MyNotificationManager object MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext()); //creating an intent for the notification Intent intent = new Intent(getApplicationContext(), recentchat.class); //if there is no image if (imageUrl.equals("null")) { //displaying small notification mNotificationManager.showSmallNotification(title, message, intent); } else { //if there is an image //displaying a big notification mNotificationManager.showBigNotification(title, message, imageUrl, intent); } } catch (JSONException e) { Log.e(TAG, "Json Exception: " + e.getMessage()); } catch (Exception e) { Log.e(TAG, "Exception: " + e.getMessage()); } } public String getContactDisplayNameByNumber(String number) { Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); String name = "?"; ContentResolver contentResolver = getContentResolver(); Cursor contactLookup = contentResolver.query(uri, new String[] { BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null); try { if (contactLookup != null && contactLookup.getCount() > 0) { contactLookup.moveToNext(); name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)); //String contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID)); hashMap.put(name, number); } } finally { if (contactLookup != null) { contactLookup.close(); } } return name; } public static String getLastThree(String myString) { if (myString.length() > 10) return myString.substring(myString.length() - 10); else return myString; } /* public void getdp() { mFirebaseMessagesChatconnectioncheck.addValueEventListener(new ValueEventListener() { @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB) @Override public void onDataChange(DataSnapshot dataSnapshot1) { for (DataSnapshot connectionchild : dataSnapshot1.getChildren()) { if (connectionchild.getKey().contains(title1)) { Log.e("title bol", title1); connectionstatus2 = dataSnapshot1.child(title1).child(ReferenceUrl.image).getValue().toString(); imageAsBytes = Base64.decode(connectionstatus2.getBytes(), Base64.DEFAULT); image = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length); System.out.println("Downloaded image with length: " + imageAsBytes.length); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 40, bytes); // String root = getFilesDir(); String filepath = Environment.getExternalStorageDirectory().getPath(); File myDir = new File(filepath + "/.HereamI"); Log.e("file", myDir.toString()); myDir.mkdirs(); String fname = title + ".jpg"; //you can create a new file name "test.jpg" in sdcard folder. File file = new File(myDir, fname); if (file.exists()) file.delete(); try { FileOutputStream out = new FileOutputStream(file); out.write(bytes.toByteArray()); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } } } @Override public void onCancelled(FirebaseError firebaseError) { } }); } */ }