├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── app ├── .gitignore ├── build.gradle ├── google-services.json ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── net │ │ └── simplifiedcoding │ │ └── firebasecloudmessaging │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── net │ │ │ └── simplifiedcoding │ │ │ └── firebasecloudmessaging │ │ │ ├── ActivitySendPushNotification.java │ │ │ ├── EndPoints.java │ │ │ ├── MainActivity.java │ │ │ ├── MyFirebaseInstanceIDService.java │ │ │ ├── MyFirebaseMessagingService.java │ │ │ ├── MyNotificationManager.java │ │ │ ├── MyVolley.java │ │ │ └── SharedPrefManager.java │ └── res │ │ ├── layout │ │ ├── activity_main.xml │ │ └── activity_send_push_notification.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── net │ └── simplifiedcoding │ └── firebasecloudmessaging │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 24 5 | buildToolsVersion "24.0.2" 6 | defaultConfig { 7 | applicationId "net.simplifiedcoding.firebasecloudmessaging" 8 | minSdkVersion 18 9 | targetSdkVersion 24 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:24.2.1' 28 | compile 'com.google.firebase:firebase-messaging:9.4.0' 29 | compile 'com.android.volley:volley:1.0.0' 30 | testCompile 'junit:junit:4.12' 31 | } 32 | 33 | 34 | 35 | apply plugin: 'com.google.gms.google-services' -------------------------------------------------------------------------------- /app/google-services.json: -------------------------------------------------------------------------------- 1 | { 2 | "project_info": { 3 | "project_number": "691947741164", 4 | "firebase_url": "https://fcmsimplifiedcoding-41a16.firebaseio.com", 5 | "project_id": "fcmsimplifiedcoding-41a16", 6 | "storage_bucket": "fcmsimplifiedcoding-41a16.appspot.com" 7 | }, 8 | "client": [ 9 | { 10 | "client_info": { 11 | "mobilesdk_app_id": "1:691947741164:android:6fd8868894e92239", 12 | "android_client_info": { 13 | "package_name": "net.simplifiedcoding.firebasecloudmessaging" 14 | } 15 | }, 16 | "oauth_client": [ 17 | { 18 | "client_id": "691947741164-5ictrfs16pllitgat7duo3kg15jja03q.apps.googleusercontent.com", 19 | "client_type": 1, 20 | "android_info": { 21 | "package_name": "net.simplifiedcoding.firebasecloudmessaging", 22 | "certificate_hash": "034860729E02CCF968EC7093B280C25AB4104EE2" 23 | } 24 | }, 25 | { 26 | "client_id": "691947741164-ekgp8i8rf5snea0ol4ofs3c2ddi30heq.apps.googleusercontent.com", 27 | "client_type": 3 28 | } 29 | ], 30 | "api_key": [ 31 | { 32 | "current_key": "AIzaSyAUGmuZQ0fUydD2AhCr7cLxmrrbSEsyZLc" 33 | } 34 | ], 35 | "services": { 36 | "analytics_service": { 37 | "status": 1 38 | }, 39 | "appinvite_service": { 40 | "status": 2, 41 | "other_platform_oauth_client": [ 42 | { 43 | "client_id": "691947741164-ekgp8i8rf5snea0ol4ofs3c2ddi30heq.apps.googleusercontent.com", 44 | "client_type": 3 45 | } 46 | ] 47 | }, 48 | "ads_service": { 49 | "status": 2 50 | } 51 | } 52 | } 53 | ], 54 | "configuration_version": "1" 55 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/Belal/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/net/simplifiedcoding/firebasecloudmessaging/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.firebasecloudmessaging; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("net.simplifiedcoding.firebasecloudmessaging", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/firebasecloudmessaging/ActivitySendPushNotification.java: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.firebasecloudmessaging; 2 | 3 | import android.app.ProgressDialog; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.text.TextUtils; 7 | import android.view.View; 8 | import android.widget.ArrayAdapter; 9 | import android.widget.Button; 10 | import android.widget.EditText; 11 | import android.widget.RadioGroup; 12 | import android.widget.Spinner; 13 | import android.widget.Toast; 14 | 15 | import com.android.volley.AuthFailureError; 16 | import com.android.volley.Request; 17 | import com.android.volley.Response; 18 | import com.android.volley.VolleyError; 19 | import com.android.volley.toolbox.StringRequest; 20 | 21 | import org.json.JSONArray; 22 | import org.json.JSONException; 23 | import org.json.JSONObject; 24 | 25 | import java.util.ArrayList; 26 | import java.util.HashMap; 27 | import java.util.List; 28 | import java.util.Map; 29 | 30 | public class ActivitySendPushNotification extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener { 31 | 32 | private Button buttonSendPush; 33 | private RadioGroup radioGroup; 34 | private Spinner spinner; 35 | private ProgressDialog progressDialog; 36 | 37 | private EditText editTextTitle, editTextMessage, editTextImage; 38 | 39 | private boolean isSendAllChecked; 40 | private List devices; 41 | 42 | 43 | @Override 44 | protected void onCreate(Bundle savedInstanceState) { 45 | super.onCreate(savedInstanceState); 46 | setContentView(R.layout.activity_send_push_notification); 47 | 48 | radioGroup = (RadioGroup) findViewById(R.id.radioGroup); 49 | spinner = (Spinner) findViewById(R.id.spinnerDevices); 50 | buttonSendPush = (Button) findViewById(R.id.buttonSendPush); 51 | 52 | editTextTitle = (EditText) findViewById(R.id.editTextTitle); 53 | editTextMessage = (EditText) findViewById(R.id.editTextMessage); 54 | editTextImage = (EditText) findViewById(R.id.editTextImageUrl); 55 | 56 | devices = new ArrayList<>(); 57 | 58 | radioGroup.setOnCheckedChangeListener(this); 59 | buttonSendPush.setOnClickListener(this); 60 | 61 | loadRegisteredDevices(); 62 | } 63 | 64 | //method to load all the devices from database 65 | private void loadRegisteredDevices() { 66 | progressDialog = new ProgressDialog(this); 67 | progressDialog.setMessage("Fetching Devices..."); 68 | progressDialog.show(); 69 | 70 | StringRequest stringRequest = new StringRequest(Request.Method.GET, EndPoints.URL_FETCH_DEVICES, 71 | new Response.Listener() { 72 | @Override 73 | public void onResponse(String response) { 74 | progressDialog.dismiss(); 75 | JSONObject obj = null; 76 | try { 77 | obj = new JSONObject(response); 78 | if (!obj.getBoolean("error")) { 79 | JSONArray jsonDevices = obj.getJSONArray("devices"); 80 | 81 | for (int i = 0; i < jsonDevices.length(); i++) { 82 | JSONObject d = jsonDevices.getJSONObject(i); 83 | devices.add(d.getString("email")); 84 | } 85 | 86 | ArrayAdapter arrayAdapter = new ArrayAdapter( 87 | ActivitySendPushNotification.this, 88 | android.R.layout.simple_spinner_dropdown_item, 89 | devices); 90 | 91 | spinner.setAdapter(arrayAdapter); 92 | } 93 | } catch (JSONException e) { 94 | e.printStackTrace(); 95 | } 96 | } 97 | }, 98 | new Response.ErrorListener() { 99 | @Override 100 | public void onErrorResponse(VolleyError error) { 101 | 102 | } 103 | }) { 104 | 105 | }; 106 | MyVolley.getInstance(this).addToRequestQueue(stringRequest); 107 | } 108 | 109 | //this method will send the push 110 | //from here we will call sendMultiple() or sendSingle() push method 111 | //depending on the selection 112 | private void sendPush() { 113 | if (isSendAllChecked) { 114 | sendMultiplePush(); 115 | } else { 116 | sendSinglePush(); 117 | } 118 | } 119 | 120 | private void sendMultiplePush() { 121 | final String title = editTextTitle.getText().toString(); 122 | final String message = editTextMessage.getText().toString(); 123 | final String image = editTextImage.getText().toString(); 124 | 125 | progressDialog.setMessage("Sending Push"); 126 | progressDialog.show(); 127 | 128 | StringRequest stringRequest = new StringRequest(Request.Method.POST, EndPoints.URL_SEND_MULTIPLE_PUSH, 129 | new Response.Listener() { 130 | @Override 131 | public void onResponse(String response) { 132 | progressDialog.dismiss(); 133 | 134 | Toast.makeText(ActivitySendPushNotification.this, response, Toast.LENGTH_LONG).show(); 135 | } 136 | }, 137 | new Response.ErrorListener() { 138 | @Override 139 | public void onErrorResponse(VolleyError error) { 140 | 141 | } 142 | }) { 143 | @Override 144 | protected Map getParams() throws AuthFailureError { 145 | Map params = new HashMap<>(); 146 | params.put("title", title); 147 | params.put("message", message); 148 | 149 | if (!TextUtils.isEmpty(image)) 150 | params.put("image", image); 151 | return params; 152 | } 153 | }; 154 | 155 | MyVolley.getInstance(this).addToRequestQueue(stringRequest); 156 | } 157 | 158 | private void sendSinglePush() { 159 | final String title = editTextTitle.getText().toString(); 160 | final String message = editTextMessage.getText().toString(); 161 | final String image = editTextImage.getText().toString(); 162 | final String email = spinner.getSelectedItem().toString(); 163 | 164 | progressDialog.setMessage("Sending Push"); 165 | progressDialog.show(); 166 | 167 | StringRequest stringRequest = new StringRequest(Request.Method.POST, EndPoints.URL_SEND_SINGLE_PUSH, 168 | new Response.Listener() { 169 | @Override 170 | public void onResponse(String response) { 171 | progressDialog.dismiss(); 172 | 173 | Toast.makeText(ActivitySendPushNotification.this, response, Toast.LENGTH_LONG).show(); 174 | } 175 | }, 176 | new Response.ErrorListener() { 177 | @Override 178 | public void onErrorResponse(VolleyError error) { 179 | 180 | } 181 | }) { 182 | @Override 183 | protected Map getParams() throws AuthFailureError { 184 | Map params = new HashMap<>(); 185 | params.put("title", title); 186 | params.put("message", message); 187 | 188 | if (!TextUtils.isEmpty(image)) 189 | params.put("image", image); 190 | 191 | params.put("email", email); 192 | return params; 193 | } 194 | }; 195 | 196 | MyVolley.getInstance(this).addToRequestQueue(stringRequest); 197 | } 198 | 199 | @Override 200 | public void onCheckedChanged(RadioGroup radioGroup, int i) { 201 | switch (radioGroup.getCheckedRadioButtonId()) { 202 | case R.id.radioButtonSendAll: 203 | isSendAllChecked = true; 204 | spinner.setEnabled(false); 205 | break; 206 | 207 | case R.id.radioButtonSendOne: 208 | isSendAllChecked = false; 209 | spinner.setEnabled(true); 210 | break; 211 | 212 | } 213 | } 214 | 215 | @Override 216 | public void onClick(View view) { 217 | //calling the method send push on button click 218 | sendPush(); 219 | } 220 | } 221 | -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/firebasecloudmessaging/EndPoints.java: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.firebasecloudmessaging; 2 | 3 | /** 4 | * Created by Belal on 04/11/16. 5 | */ 6 | 7 | public class EndPoints { 8 | public static final String URL_REGISTER_DEVICE = "http://192.168.1.101/FcmExample/RegisterDevice.php"; 9 | public static final String URL_SEND_SINGLE_PUSH = "http://192.168.1.101/FcmExample/sendSinglePush.php"; 10 | public static final String URL_SEND_MULTIPLE_PUSH = "http://192.168.1.101/FcmExample/sendMultiplePush.php"; 11 | public static final String URL_FETCH_DEVICES = "http://192.168.1.101/FcmExample/GetRegisteredDevices.php"; 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/firebasecloudmessaging/MainActivity.java: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.firebasecloudmessaging; 2 | 3 | import android.app.ProgressDialog; 4 | import android.content.Intent; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.os.Bundle; 7 | import android.view.View; 8 | import android.widget.Button; 9 | import android.widget.EditText; 10 | import android.widget.ProgressBar; 11 | import android.widget.TextView; 12 | import android.widget.Toast; 13 | 14 | import com.android.volley.AuthFailureError; 15 | import com.android.volley.Request; 16 | import com.android.volley.RequestQueue; 17 | import com.android.volley.Response; 18 | import com.android.volley.VolleyError; 19 | import com.android.volley.toolbox.StringRequest; 20 | import com.android.volley.toolbox.Volley; 21 | 22 | import org.json.JSONException; 23 | import org.json.JSONObject; 24 | 25 | import java.util.HashMap; 26 | import java.util.Map; 27 | 28 | public class MainActivity extends AppCompatActivity implements View.OnClickListener { 29 | 30 | //defining views 31 | private Button buttonSendPush; 32 | private Button buttonRegister; 33 | private EditText editTextEmail; 34 | private ProgressDialog progressDialog; 35 | 36 | @Override 37 | protected void onCreate(Bundle savedInstanceState) { 38 | super.onCreate(savedInstanceState); 39 | setContentView(R.layout.activity_main); 40 | 41 | //getting views from xml 42 | editTextEmail = (EditText) findViewById(R.id.editTextEmail); 43 | buttonRegister = (Button) findViewById(R.id.buttonRegister); 44 | buttonSendPush = (Button) findViewById(R.id.buttonSendNotification); 45 | 46 | //adding listener to view 47 | buttonRegister.setOnClickListener(this); 48 | buttonSendPush.setOnClickListener(this); 49 | } 50 | 51 | //storing token to mysql server 52 | private void sendTokenToServer() { 53 | progressDialog = new ProgressDialog(this); 54 | progressDialog.setMessage("Registering Device..."); 55 | progressDialog.show(); 56 | 57 | final String token = SharedPrefManager.getInstance(this).getDeviceToken(); 58 | final String email = editTextEmail.getText().toString(); 59 | 60 | if (token == null) { 61 | progressDialog.dismiss(); 62 | Toast.makeText(this, "Token not generated", Toast.LENGTH_LONG).show(); 63 | return; 64 | } 65 | 66 | StringRequest stringRequest = new StringRequest(Request.Method.POST, EndPoints.URL_REGISTER_DEVICE, 67 | new Response.Listener() { 68 | @Override 69 | public void onResponse(String response) { 70 | progressDialog.dismiss(); 71 | try { 72 | JSONObject obj = new JSONObject(response); 73 | Toast.makeText(MainActivity.this, obj.getString("message"), Toast.LENGTH_LONG).show(); 74 | } catch (JSONException e) { 75 | e.printStackTrace(); 76 | } 77 | } 78 | }, 79 | new Response.ErrorListener() { 80 | @Override 81 | public void onErrorResponse(VolleyError error) { 82 | progressDialog.dismiss(); 83 | Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG).show(); 84 | } 85 | }) { 86 | 87 | @Override 88 | protected Map getParams() throws AuthFailureError { 89 | Map params = new HashMap<>(); 90 | params.put("email", email); 91 | params.put("token", token); 92 | return params; 93 | } 94 | }; 95 | MyVolley.getInstance(this).addToRequestQueue(stringRequest); 96 | } 97 | 98 | @Override 99 | public void onClick(View view) { 100 | if (view == buttonRegister) { 101 | sendTokenToServer(); 102 | } 103 | 104 | //starting send notification activity 105 | if (view == buttonSendPush) { 106 | startActivity(new Intent(this, ActivitySendPushNotification.class)); 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/firebasecloudmessaging/MyFirebaseInstanceIDService.java: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.firebasecloudmessaging; 2 | 3 | import android.util.Log; 4 | 5 | import com.google.firebase.iid.FirebaseInstanceId; 6 | import com.google.firebase.iid.FirebaseInstanceIdService; 7 | 8 | /** 9 | * Created by Belal on 03/11/16. 10 | */ 11 | 12 | 13 | public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { 14 | 15 | private static final String TAG = "MyFirebaseIIDService"; 16 | 17 | @Override 18 | public void onTokenRefresh() { 19 | String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 20 | Log.d(TAG, "Refreshed token: " + refreshedToken); 21 | storeToken(refreshedToken); 22 | } 23 | 24 | private void storeToken(String token) { 25 | //saving the token on shared preferences 26 | SharedPrefManager.getInstance(getApplicationContext()).saveDeviceToken(token); 27 | } 28 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/firebasecloudmessaging/MyFirebaseMessagingService.java: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.firebasecloudmessaging; 2 | 3 | 4 | import android.content.Intent; 5 | import android.util.Log; 6 | 7 | import com.google.firebase.messaging.FirebaseMessagingService; 8 | import com.google.firebase.messaging.RemoteMessage; 9 | 10 | import org.json.JSONException; 11 | import org.json.JSONObject; 12 | 13 | /** 14 | * Created by Belal on 03/11/16. 15 | */ 16 | 17 | public class MyFirebaseMessagingService extends FirebaseMessagingService { 18 | 19 | private static final String TAG = "MyFirebaseMsgService"; 20 | 21 | @Override 22 | public void onMessageReceived(RemoteMessage remoteMessage) { 23 | if (remoteMessage.getData().size() > 0) { 24 | Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString()); 25 | try { 26 | JSONObject json = new JSONObject(remoteMessage.getData().toString()); 27 | sendPushNotification(json); 28 | } catch (Exception e) { 29 | Log.e(TAG, "Exception: " + e.getMessage()); 30 | } 31 | } 32 | } 33 | 34 | //this method will display the notification 35 | //We are passing the JSONObject that is received from 36 | //firebase cloud messaging 37 | private void sendPushNotification(JSONObject json) { 38 | //optionally we can display the json into log 39 | Log.e(TAG, "Notification JSON " + json.toString()); 40 | try { 41 | //getting the json data 42 | JSONObject data = json.getJSONObject("data"); 43 | 44 | //parsing json data 45 | String title = data.getString("title"); 46 | String message = data.getString("message"); 47 | String imageUrl = data.getString("image"); 48 | 49 | //creating MyNotificationManager object 50 | MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext()); 51 | 52 | //creating an intent for the notification 53 | Intent intent = new Intent(getApplicationContext(), MainActivity.class); 54 | 55 | //if there is no image 56 | if(imageUrl.equals("null")){ 57 | //displaying small notification 58 | mNotificationManager.showSmallNotification(title, message, intent); 59 | }else{ 60 | //if there is an image 61 | //displaying a big notification 62 | mNotificationManager.showBigNotification(title, message, imageUrl, intent); 63 | } 64 | } catch (JSONException e) { 65 | Log.e(TAG, "Json Exception: " + e.getMessage()); 66 | } catch (Exception e) { 67 | Log.e(TAG, "Exception: " + e.getMessage()); 68 | } 69 | } 70 | 71 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/firebasecloudmessaging/MyNotificationManager.java: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.firebasecloudmessaging; 2 | 3 | /** 4 | * Created by Belal on 03/11/16. 5 | */ 6 | 7 | import android.app.Notification; 8 | import android.app.NotificationManager; 9 | import android.app.PendingIntent; 10 | import android.content.Context; 11 | import android.content.Intent; 12 | import android.graphics.Bitmap; 13 | import android.graphics.BitmapFactory; 14 | import android.support.v4.app.NotificationCompat; 15 | import android.text.Html; 16 | 17 | import java.io.IOException; 18 | import java.io.InputStream; 19 | import java.net.HttpURLConnection; 20 | import java.net.URL; 21 | 22 | 23 | /** 24 | * Created by Ravi on 31/03/15. 25 | */ 26 | 27 | public class MyNotificationManager { 28 | 29 | public static final int ID_BIG_NOTIFICATION = 234; 30 | public static final int ID_SMALL_NOTIFICATION = 235; 31 | 32 | private Context mCtx; 33 | 34 | public MyNotificationManager(Context mCtx) { 35 | this.mCtx = mCtx; 36 | } 37 | 38 | //the method will show a big notification with an image 39 | //parameters are title for message title, message for message text, url of the big image and an intent that will open 40 | //when you will tap on the notification 41 | public void showBigNotification(String title, String message, String url, Intent intent) { 42 | PendingIntent resultPendingIntent = 43 | PendingIntent.getActivity( 44 | mCtx, 45 | ID_BIG_NOTIFICATION, 46 | intent, 47 | PendingIntent.FLAG_UPDATE_CURRENT 48 | ); 49 | NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle(); 50 | bigPictureStyle.setBigContentTitle(title); 51 | bigPictureStyle.setSummaryText(Html.fromHtml(message).toString()); 52 | bigPictureStyle.bigPicture(getBitmapFromURL(url)); 53 | NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx); 54 | Notification notification; 55 | notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher).setTicker(title).setWhen(0) 56 | .setAutoCancel(true) 57 | .setContentIntent(resultPendingIntent) 58 | .setContentTitle(title) 59 | .setStyle(bigPictureStyle) 60 | .setSmallIcon(R.mipmap.ic_launcher) 61 | .setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.mipmap.ic_launcher)) 62 | .setContentText(message) 63 | .build(); 64 | 65 | notification.flags |= Notification.FLAG_AUTO_CANCEL; 66 | 67 | NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE); 68 | notificationManager.notify(ID_BIG_NOTIFICATION, notification); 69 | } 70 | 71 | //the method will show a small notification 72 | //parameters are title for message title, message for message text and an intent that will open 73 | //when you will tap on the notification 74 | public void showSmallNotification(String title, String message, Intent intent) { 75 | PendingIntent resultPendingIntent = 76 | PendingIntent.getActivity( 77 | mCtx, 78 | ID_SMALL_NOTIFICATION, 79 | intent, 80 | PendingIntent.FLAG_UPDATE_CURRENT 81 | ); 82 | 83 | 84 | NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx); 85 | Notification notification; 86 | notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher).setTicker(title).setWhen(0) 87 | .setAutoCancel(true) 88 | .setContentIntent(resultPendingIntent) 89 | .setContentTitle(title) 90 | .setSmallIcon(R.mipmap.ic_launcher) 91 | .setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.mipmap.ic_launcher)) 92 | .setContentText(message) 93 | .build(); 94 | 95 | notification.flags |= Notification.FLAG_AUTO_CANCEL; 96 | 97 | NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE); 98 | notificationManager.notify(ID_SMALL_NOTIFICATION, notification); 99 | } 100 | 101 | //The method will return Bitmap from an image URL 102 | private Bitmap getBitmapFromURL(String strURL) { 103 | try { 104 | URL url = new URL(strURL); 105 | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 106 | connection.setDoInput(true); 107 | connection.connect(); 108 | InputStream input = connection.getInputStream(); 109 | Bitmap myBitmap = BitmapFactory.decodeStream(input); 110 | return myBitmap; 111 | } catch (IOException e) { 112 | e.printStackTrace(); 113 | return null; 114 | } 115 | } 116 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/firebasecloudmessaging/MyVolley.java: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.firebasecloudmessaging; 2 | 3 | import android.content.Context; 4 | import android.graphics.Bitmap; 5 | import android.support.v4.util.LruCache; 6 | 7 | import com.android.volley.Request; 8 | import com.android.volley.RequestQueue; 9 | import com.android.volley.toolbox.ImageLoader; 10 | import com.android.volley.toolbox.Volley; 11 | 12 | /** 13 | * Created by Belal on 13/10/16. 14 | */ 15 | 16 | public class MyVolley { 17 | 18 | private static MyVolley mInstance; 19 | private RequestQueue mRequestQueue; 20 | private static Context mCtx; 21 | 22 | private MyVolley(Context context) { 23 | mCtx = context; 24 | mRequestQueue = getRequestQueue(); 25 | } 26 | 27 | public static synchronized MyVolley getInstance(Context context) { 28 | if (mInstance == null) { 29 | mInstance = new MyVolley(context); 30 | } 31 | return mInstance; 32 | } 33 | 34 | public RequestQueue getRequestQueue() { 35 | if (mRequestQueue == null) { 36 | // getApplicationContext() is key, it keeps you from leaking the 37 | // Activity or BroadcastReceiver if someone passes one in. 38 | mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext()); 39 | } 40 | return mRequestQueue; 41 | } 42 | 43 | public void addToRequestQueue(Request req) { 44 | getRequestQueue().add(req); 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/firebasecloudmessaging/SharedPrefManager.java: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.firebasecloudmessaging; 2 | 3 | import android.content.Context; 4 | import android.content.SharedPreferences; 5 | 6 | /** 7 | * Created by Belal on 03/11/16. 8 | */ 9 | 10 | public class SharedPrefManager { 11 | private static final String SHARED_PREF_NAME = "FCMSharedPref"; 12 | private static final String TAG_TOKEN = "tagtoken"; 13 | 14 | private static SharedPrefManager mInstance; 15 | private static Context mCtx; 16 | 17 | private SharedPrefManager(Context context) { 18 | mCtx = context; 19 | } 20 | 21 | public static synchronized SharedPrefManager getInstance(Context context) { 22 | if (mInstance == null) { 23 | mInstance = new SharedPrefManager(context); 24 | } 25 | return mInstance; 26 | } 27 | 28 | //this method will save the device token to shared preferences 29 | public boolean saveDeviceToken(String token){ 30 | SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE); 31 | SharedPreferences.Editor editor = sharedPreferences.edit(); 32 | editor.putString(TAG_TOKEN, token); 33 | editor.apply(); 34 | return true; 35 | } 36 | 37 | //this method will fetch the device token from shared preferences 38 | public String getDeviceToken(){ 39 | SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE); 40 | return sharedPreferences.getString(TAG_TOKEN, null); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 19 | 20 |