Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.80 KB | None | 0 0
  1. error_reporting(-1);
  2. ini_set('display_errors', 'On');
  3.  
  4. require_once __DIR__ . '/firebase.php';
  5. require_once __DIR__ . '/push.php';
  6.  
  7. $firebase = new Firebase();
  8. $push = new Push();
  9.  
  10. // optional payload
  11. $payload = array();
  12. $payload['title'] = 'Notification!';
  13. $payload['message'] = 'New PickupListed';
  14.  
  15. // notification title
  16. $title = isset($_GET['title']) ? $_GET['title'] : '';
  17.  
  18. // notification message
  19. $message = isset($_GET['message']) ? $_GET['message'] : '';
  20.  
  21. // push type - single user / topic
  22. $push_type = isset($_GET['push_type']) ? $_GET['push_type'] : '';
  23.  
  24. // whether to include to image or not
  25. $include_image = isset($_GET['include_image']) ? TRUE : FALSE;
  26.  
  27.  
  28. $push->setTitle($title);
  29. $push->setMessage($message);
  30.  
  31. if ($include_image) {
  32. $push->setImage('http://api.androidhive.info/images/minion.jpg');
  33. } else {
  34. $push->setImage('');
  35. }
  36.  
  37. $push->setIsBackground(FALSE);
  38. $push->setPayload($payload);
  39.  
  40. $json = '';
  41. $response = '';
  42.  
  43. if ($push_type == 'topic') {
  44. $json = $push->getPush();
  45. $response = $firebase->sendToTopic('global', $json);
  46. } else if ($push_type == 'individual') {
  47. $json = $push->getPush();
  48. $regId = isset($_GET['regId']) ? $_GET['regId'] : '';
  49. $response = $firebase->send($regId, $json);
  50. }
  51.  
  52. private NotificationUtils notificationUtils;
  53. @Override
  54. public void onMessageReceived(RemoteMessage remoteMessage) {
  55.  
  56. Log.e(TAG, "===========================messaging 1=======" );
  57.  
  58. Log.e(TAG, "From: " + remoteMessage.getFrom());
  59.  
  60. if (remoteMessage == null)
  61. return;
  62.  
  63. // Check if message contains a notification payload.
  64. if (remoteMessage.getNotification() != null) {
  65. Log.e(TAG, "===========================messaging 2=======" );
  66. Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
  67. handleNotification(remoteMessage.getNotification().getBody());
  68. }
  69.  
  70. // Check if message contains a data payload.
  71. if (remoteMessage.getData().size() > 0) {
  72. Log.e(TAG, "===========================messaging 3=======" );
  73. Log.e(TAG, "Data Payload: " + remoteMessage.getData());
  74.  
  75. try {
  76. JSONObject json = new JSONObject(remoteMessage.getData());
  77. handleDataMessage(json);
  78. } catch (Exception e) {
  79. Log.e(TAG, "Exception: " + e.getMessage());
  80. }
  81. }
  82. }
  83.  
  84. private void handleNotification(String message) {
  85. Log.e(TAG, "===========================messaging 4=======" );
  86.  
  87. if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
  88.  
  89. Intent intent= new Intent(this,MainActivity.class);
  90. intent.putExtra("message",message);
  91. intent.putExtra("imageUrl",imageUrl);
  92. intent.putExtra("time_stamp",timeStamp);
  93.  
  94. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  95.  
  96.  
  97. LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
  98.  
  99. final PendingIntent pendingIntent = PendingIntent.getActivity(
  100. this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
  101.  
  102.  
  103. Uri notificationSound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  104.  
  105. NotificationCompat.Builder notifiBuilder=new NotificationCompat.Builder(this)
  106. .setSmallIcon(R.mipmap.ic_launcher)
  107. .setContentTitle("NOTIFICATION !!")
  108. .setContentText(message)
  109. .setAutoCancel(true)
  110. .setSound(notificationSound)
  111. .setContentIntent(pendingIntent) ;
  112.  
  113. NotificationManager notificationManager=
  114. ( NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  115.  
  116. notificationManager.notify(0,notifiBuilder.build());
  117.  
  118.  
  119. Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
  120. pushNotification.putExtra("message", message);
  121. LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
  122.  
  123.  
  124.  
  125.  
  126. }
  127. }
  128.  
  129. private void handleDataMessage(JSONObject json) {
  130. Log.e(TAG, "===========================messaging 4=======" );
  131. Log.e(TAG, "push json: " + json.toString());
  132.  
  133.  
  134. try {
  135. JSONObject data = json.getJSONObject("data");
  136.  
  137. String title = data.getString("title");
  138. String message = data.getString("message");
  139. boolean isBackground = data.getBoolean("is_background");
  140. String imageUrl = data.getString("image");
  141. String timestamp = data.getString("timestamp");
  142. JSONObject payload = data.getJSONObject("payload");
  143.  
  144. Log.e(TAG, "title: " + title);
  145. Log.e(TAG, "message: " + message);
  146. Log.e(TAG, "isBackground: " + isBackground);
  147. Log.e(TAG, "payload: " + payload.toString());
  148. Log.e(TAG, "imageUrl: " + imageUrl);
  149. Log.e(TAG, "timestamp: " + timestamp);
  150.  
  151.  
  152. if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
  153. // app is in foreground, broadcast the push message
  154.  
  155. Intent intent= new Intent(this,MainActivity.class);
  156. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  157. LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
  158. final PendingIntent pendingIntent = PendingIntent.getActivity(
  159. this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
  160.  
  161.  
  162.  
  163. Uri notificationSound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  164.  
  165. NotificationCompat.Builder notifiBuilder=new NotificationCompat.Builder(this)
  166. .setSmallIcon(R.mipmap.ic_launcher)
  167. .setContentTitle("NOTIFICATION data message!!")
  168. .setContentText(message)
  169. .addAction(R.mipmap.ic_accept,"ACCEPT",pendingIntent)
  170. .addAction(R.mipmap.ic_decline,"DECLINE",pendingIntent)
  171. .setAutoCancel(true)
  172. .setSound(notificationSound)
  173. .setContentIntent(pendingIntent) ;
  174.  
  175. NotificationManager notificationManager=
  176. ( NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  177.  
  178. notificationManager.notify(0,notifiBuilder.build());
  179.  
  180.  
  181. Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
  182. pushNotification.putExtra("message", message);
  183. LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
  184.  
  185. // play notification sound
  186. NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
  187. notificationUtils.playNotificationSound();
  188. } else {
  189.  
  190. Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
  191. resultIntent.putExtra("message", message);
  192.  
  193. // check for image attachment
  194. if (TextUtils.isEmpty(imageUrl)) {
  195. showNotificationMessage(getApplicationContext(), title, message, resultIntent);
  196. } else {
  197.  
  198. showNotificationMessageWithBigImage(getApplicationContext(), title, message, resultIntent, imageUrl);
  199. }
  200. }
  201. } catch (JSONException e) {
  202. Log.e(TAG, "Json Exception: " + e.getMessage());
  203. } catch (Exception e) {
  204. Log.e(TAG, "Exception: " + e.getMessage());
  205. }
  206. }
  207.  
  208.  
  209.  
  210. private void showNotificationMessage(Context context, String title, String message, Intent intent) {
  211. Log.e(TAG, "===========================messaging 5=======" );
  212. notificationUtils = new NotificationUtils(context);
  213. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  214. notificationUtils.showNotificationMessage(title, message, intent);
  215. }
  216.  
  217. Request:
  218. {"data":{"title":"hey all","is_background":false,"message":"demo","image":"","timestamp":"2017-07-27 12:22:41"}}
  219.  
  220.  
  221. Response:
  222. "{"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