Example 1: web push notifications example
Please take a look here
https://firebase.google.com/docs/cloud-messaging/js/client
and my github FREE source code push web notification demo
https://github.com/zidane168/WebPushDemo
Example 2: push notification
public function push($device_data, $message, $push_params) {
$ch = curl_init();
$this->set_credential(true);
$url = $this->server_feedback_url;
$headers = array(
'Connection: keep-alive',
'Authorization: key=' . $this->server_key,
'Content-Type: application/json'
);
$fields = array();
$failed_case = array();
$succeed_case = array();
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$fields = array(
'registration_ids' => $device_data,
'notification' => array(
'body' => $message['notification']['body'],
'title' => $message['notification']['title'],
),
'priority' => 'high',
'data' => isset($push_params['custom_data']) ? $push_params['custom_data'] : '',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
$temp = json_decode($result, true);
if ($temp['failure'] == 0) {
$succeed_case = $device_data;
$failed_case = array();
} else {
$index = 0;
foreach ($temp['results'] as $value) {
if (isset($value['error'])) {
$failed_case[] = $device_data[$index];
} else {
$succeed_case[] = $device_data[$index];
}
$index = $index + 1;
}
}
$pushed = array(
'status' => true,
'params' => array(
'result' => $result,
'succeed' => $succeed_case,
'failed' => $failed_case,
),
);
} catch (Exception $e) {
$pushed = array(
'status' => false,
'params' => array(),
'error_messages' => $e->getMessage(),
);
} finally {
curl_close($ch);
}
return $pushed;
}
public function set_credential($sandbox = true) {
if ($sandbox === true) {
$this->server_key = Environment::read('push.server_key');
$this->sender_id = Environment::read('push.sender_id');
$this->server_feedback_url = Environment::read('push.server_feedback_url');
}
}
Example 3: create notification android
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(yourContext.getApplicationContext(), "notify_001");
Intent ii = new Intent(yourContext.getApplicationContext(), YourMainActivty.class);
PendingIntent pendingIntent = PendingIntent.getActivity(yourContext, 0, ii, 0);
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText(notificationsTextDetailMode);
bigText.setBigContentTitle(notificationTitleDetailMode);
bigText.setSummaryText(usuallyAppVersionOrNumberOfNotifications);
mBuilder.setContentIntent(pendingIntent);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle(notificationTitle);
mBuilder.setContentText(notificationText);
mBuilder.setPriority(Notification.PRIORITY_MAX);
mBuilder.setStyle(bigText);
NotificationManager mNotificationManager = (NotificationManager) yourContext.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("notify_001",
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannel(channel);
}
if (mNotificationManager != null) {
mNotificationManager.notify(0, mBuilder.build());
}
Example 4: web push showing 2 notification fcm
self.registration.hideNotification();