Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- error_reporting(-1);
- ini_set('display_errors', 'On');
- require_once __DIR__ . '/firebase.php';
- require_once __DIR__ . '/push.php';
- $firebase = new Firebase();
- $push = new Push();
- // optional payload
- $payload = array();
- $payload['title'] = 'Notification!';
- $payload['message'] = 'New PickupListed';
- // notification title
- $title = isset($_GET['title']) ? $_GET['title'] : '';
- // notification message
- $message = isset($_GET['message']) ? $_GET['message'] : '';
- // push type - single user / topic
- $push_type = isset($_GET['push_type']) ? $_GET['push_type'] : '';
- // whether to include to image or not
- $include_image = isset($_GET['include_image']) ? TRUE : FALSE;
- $push->setTitle($title);
- $push->setMessage($message);
- if ($include_image) {
- $push->setImage('http://api.androidhive.info/images/minion.jpg');
- } else {
- $push->setImage('');
- }
- $push->setIsBackground(FALSE);
- $push->setPayload($payload);
- $json = '';
- $response = '';
- if ($push_type == 'topic') {
- $json = $push->getPush();
- $response = $firebase->sendToTopic('global', $json);
- } else if ($push_type == 'individual') {
- $json = $push->getPush();
- $regId = isset($_GET['regId']) ? $_GET['regId'] : '';
- $response = $firebase->send($regId, $json);
- }
- private NotificationUtils notificationUtils;
- @Override
- public void onMessageReceived(RemoteMessage remoteMessage) {
- Log.e(TAG, "===========================messaging 1=======" );
- Log.e(TAG, "From: " + remoteMessage.getFrom());
- if (remoteMessage == null)
- return;
- // Check if message contains a notification payload.
- if (remoteMessage.getNotification() != null) {
- Log.e(TAG, "===========================messaging 2=======" );
- Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
- handleNotification(remoteMessage.getNotification().getBody());
- }
- // Check if message contains a data payload.
- if (remoteMessage.getData().size() > 0) {
- Log.e(TAG, "===========================messaging 3=======" );
- Log.e(TAG, "Data Payload: " + remoteMessage.getData());
- try {
- JSONObject json = new JSONObject(remoteMessage.getData());
- handleDataMessage(json);
- } catch (Exception e) {
- Log.e(TAG, "Exception: " + e.getMessage());
- }
- }
- }
- private void handleNotification(String message) {
- Log.e(TAG, "===========================messaging 4=======" );
- if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
- Intent intent= new Intent(this,MainActivity.class);
- intent.putExtra("message",message);
- intent.putExtra("imageUrl",imageUrl);
- intent.putExtra("time_stamp",timeStamp);
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
- final PendingIntent pendingIntent = PendingIntent.getActivity(
- this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
- Uri notificationSound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
- NotificationCompat.Builder notifiBuilder=new NotificationCompat.Builder(this)
- .setSmallIcon(R.mipmap.ic_launcher)
- .setContentTitle("NOTIFICATION !!")
- .setContentText(message)
- .setAutoCancel(true)
- .setSound(notificationSound)
- .setContentIntent(pendingIntent) ;
- NotificationManager notificationManager=
- ( NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- notificationManager.notify(0,notifiBuilder.build());
- Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
- pushNotification.putExtra("message", message);
- LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
- }
- }
- private void handleDataMessage(JSONObject json) {
- Log.e(TAG, "===========================messaging 4=======" );
- Log.e(TAG, "push json: " + json.toString());
- try {
- JSONObject data = json.getJSONObject("data");
- String title = data.getString("title");
- String message = data.getString("message");
- boolean isBackground = data.getBoolean("is_background");
- String imageUrl = data.getString("image");
- String timestamp = data.getString("timestamp");
- JSONObject payload = data.getJSONObject("payload");
- Log.e(TAG, "title: " + title);
- Log.e(TAG, "message: " + message);
- Log.e(TAG, "isBackground: " + isBackground);
- Log.e(TAG, "payload: " + payload.toString());
- Log.e(TAG, "imageUrl: " + imageUrl);
- Log.e(TAG, "timestamp: " + timestamp);
- if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
- // app is in foreground, broadcast the push message
- Intent intent= new Intent(this,MainActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
- final PendingIntent pendingIntent = PendingIntent.getActivity(
- this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
- Uri notificationSound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
- NotificationCompat.Builder notifiBuilder=new NotificationCompat.Builder(this)
- .setSmallIcon(R.mipmap.ic_launcher)
- .setContentTitle("NOTIFICATION data message!!")
- .setContentText(message)
- .addAction(R.mipmap.ic_accept,"ACCEPT",pendingIntent)
- .addAction(R.mipmap.ic_decline,"DECLINE",pendingIntent)
- .setAutoCancel(true)
- .setSound(notificationSound)
- .setContentIntent(pendingIntent) ;
- NotificationManager notificationManager=
- ( NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- notificationManager.notify(0,notifiBuilder.build());
- Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
- pushNotification.putExtra("message", message);
- LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
- // play notification sound
- NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
- notificationUtils.playNotificationSound();
- } else {
- Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
- resultIntent.putExtra("message", message);
- // check for image attachment
- if (TextUtils.isEmpty(imageUrl)) {
- showNotificationMessage(getApplicationContext(), title, message, resultIntent);
- } else {
- showNotificationMessageWithBigImage(getApplicationContext(), title, message, resultIntent, imageUrl);
- }
- }
- } catch (JSONException e) {
- Log.e(TAG, "Json Exception: " + e.getMessage());
- } catch (Exception e) {
- Log.e(TAG, "Exception: " + e.getMessage());
- }
- }
- private void showNotificationMessage(Context context, String title, String message, Intent intent) {
- Log.e(TAG, "===========================messaging 5=======" );
- notificationUtils = new NotificationUtils(context);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
- notificationUtils.showNotificationMessage(title, message, intent);
- }
- Request:
- {"data":{"title":"hey all","is_background":false,"message":"demo","image":"","timestamp":"2017-07-27 12:22:41"}}
- Response:
- "{"multicast_id":8934845196536440484,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1501158164597153%293f606ef9fd7ecd"}]}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement