send notification android code example

Example 1: php send ios push notification

<?php

$deviceToken = '8845ba7c41e95e12caea6381ea6f01b5cd7b59a52feb9005e0727a65a4105dc2a0';

$passphrase = '';

$message = 'Your message';


$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;


$body['aps'] = array(
    'alert' => array(
        'body' => $message,
        'action-loc-key' => 'Bango App',
    ),
    'badge' => 2,
    'sound' => 'oven.caf',
    );

$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;


$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL;

fclose($fp);

Example 2: android create notification

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); //detail mode is the "expanded" notification
bigText.setBigContentTitle(notificationTitleDetailMode);
bigText.setSummaryText(usuallyAppVersionOrNumberOfNotifications); //small text under notification

mBuilder.setContentIntent(pendingIntent);
mBuilder.setSmallIcon(R.mipmap.ic_launcher); //notification icon
mBuilder.setContentTitle(notificationTitle); //main title
mBuilder.setContentText(notificationText); //main text when you "haven't expanded" the notification yet
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());
}

Tags:

Php Example