package ru.pyur.loga; import android.util.Log; import com.google.firebase.iid.FirebaseInstanceId; import com.google.firebase.iid.FirebaseInstanceIdService; public class TestFirebaseInstanceIdService extends FirebaseInstanceIdService { public static final String TAG = "TestFbseInstIdSvc"; @Override public void onTokenRefresh() { String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG, "Refreshed token: " + refreshedToken); //~sendRegistrationToServer(refreshedToken); } }
package ru.pyur.loga; import android.app.Notification; import android.app.NotificationManager; import android.content.Context; import android.support.v4.app.NotificationCompat; import android.util.Log; import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; import static ru.pyur.loga.AcMain.context; public class TestFirebaseMessagingService extends FirebaseMessagingService { public static final String TAG = "TestFbseMsgngSvc"; @Override public void onMessageReceived(RemoteMessage remoteMessage) { Log.d(TAG, "From: " + remoteMessage.getFrom()); if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Message data payload: " + remoteMessage.getData()); String val1 = remoteMessage.getData().get("val1"); String val2 = remoteMessage.getData().get("val2"); String val3 = remoteMessage.getData().get("val3"); int color = (1<<16)|(1<<8)|(0); ShowNotification(val1, val2, color); } if (remoteMessage.getNotification() != null) { Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); } } @Override public void onDeletedMessages() { // In some situations, FCM may not deliver a message. This occurs when there are too many messages (>100) pending for your app on a particular device // at the time it connects or if the device hasn't connected to FCM in more than one month. In these cases, you may receive a callback // to FirebaseMessagingService.onDeletedMessages() When the app instance receives this callback, it should perform a full sync with your app server. // If you haven't sent a message to the app on that device within the last 4 weeks, FCM won't call onDeletedMessages(). } void ShowNotification(String title, String text, int color) { NotificationCompat.Builder mNotify = new NotificationCompat.Builder(context, ""); mNotify.setLights(color, 100, 200); mNotify.setSmallIcon(R.drawable.service_icon); mNotify.setContentTitle(title); mNotify.setContentText(text); mNotify.setDefaults(Notification.DEFAULT_SOUND); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); int mId = 1001; try { mNotificationManager.notify(mId, mNotify.build()); } catch (Exception e) { e.printStackTrace(); } } }
<service android:name=".TestFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service> <service android:name=".TestFirebaseInstanceIdService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service>
<?php // ------------------------ test fcm send. legacy ------------------------ // $socket = @fsockopen('ssl://fcm.googleapis.com', 443, $errno, $errstr, 10); if (!$socket) die('error: remote host is unreachable.'); // ---- ---- // $payload = '{ "to" : "cGAFgPJGf-s:APA91bF**...**aEVM17c9peqZ", "notification" : { "title" : " ", "body" : "(Legacy API) !", "sound": "default" } }'; // // ---- ---- // $payload = '{ "to" : "cGAFgPJGf-s:APA91bF**...**aEVM17c9peqZ", "data":{ "val1" : " ", "val2" : "(Legacy API) !", "val3" : "- " } }'; $send = ''; $send .= 'POST /fcm/send HTTP/1.1'."\r\n"; $send .= 'Host: fcm.googleapis.com'."\r\n"; $send .= 'Connection: close'."\r\n"; $send .= 'Content-Type: application/json'."\r\n"; $send .= 'Authorization: key=AIzaSy***************************IPSnjk'."\r\n"; $send .= 'Content-Length: '.strlen($payload)."\r\n"; $send .= "\r\n"; $send .=$payload; $result = fwrite($socket, $send); $receive = ''; while (!feof($socket)) $receive .= fread($socket, 8192); fclose($socket); echo '<pre>'.$receive.'</pre>'; ?>
<?php // ------------------------ test fcm send. modern ------------------------ // // -- 1. JWT -- // $JWT_header = base64_encode('{"alg":"RS256","typ":"JWT"}'); $issue_time = time(); $JWT_claim_set = base64_encode( '{"iss":"firebase-adminsdk-mvxyi@<your-project>.iam.gserviceaccount.com",'. '"scope":"https://www.googleapis.com/auth/firebase.messaging",'. '"aud":"https://www.googleapis.com/oauth2/v4/token",'. '"exp":'.($issue_time + 3600).','. '"iat":'.$issue_time.'}'); // . $private_key = ' -----BEGIN PRIVATE KEY----- MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCwR1biSUCv4J4W **************************************************************** **************************************************************** ... **************************************************************** teTJImCT6sg7go7toh2ODfaPmeI0nA/LwSjzWs0b8gdIYPT5fAsvfQiND0vu/M3V 7C/z/SmIKeIcfOYrcbWQwTs= -----END PRIVATE KEY----- '; $data = $JWT_header.'.'.$JWT_claim_set; $binary_signature = ''; openssl_sign($data, $binary_signature, $private_key, 'SHA256'); $JWT_signature = base64_encode($binary_signature); $JWT = $JWT_header.'.'.$JWT_claim_set.'.'.$JWT_signature; // -- 2. -- // $socket = @fsockopen('ssl://www.googleapis.com', 443, $errno, $errstr, 10); if (!$socket) die('error: remote host is unreachable.'); $payload = 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion='.rawurlencode($JWT); $send = ''; $send .= 'POST /oauth2/v4/token HTTP/1.1'."\r\n"; $send .= 'Host: www.googleapis.com'."\r\n"; $send .= 'Connection: close'."\r\n"; $send .= 'Content-Type: application/x-www-form-urlencoded'."\r\n"; $send .= 'Content-Length: '.strlen($payload)."\r\n"; $send .= "\r\n"; $send .= $payload; $result = fwrite($socket, $send); $receive = ''; while (!feof($socket)) $receive .= fread($socket, 8192); fclose($socket); echo '<pre>'.$receive.'</pre>'; // -- parse answer JSON (lame) -- // $line = explode("\r\n", $receive); if ($line[0] != 'HTTP/1.1 200 OK') die($line[0]); $pos = FALSE; if (($pos = strpos($receive, "\r\n\r\n", 0)) !== FALSE ) { if (($pos = strpos($receive, "{", $pos+4)) !== FALSE ) { if (($pose = strpos($receive, "}", $pos+1)) !== FALSE ) { $post = substr($receive, $pos, ($pose - $pos+1) ); $aw = json_decode($post, TRUE); $access_token = $aw['access_token']; } else die('} not found.'); } else die('{ not found.'); } else die('\r\n\r\n not found.'); // -- 3. Firebase -- // $socket = @fsockopen('ssl://fcm.googleapis.com', 443, $errno, $errstr, 10); if (!$socket) die('error: remote host is unreachable.'); $payload = '{ "message":{ "token" : "cGAFgPJGf-s:APA91bF**...**aEVM17c9peqZ", "notification" : { "title" : " ", "body" : "(Modern API) Firebase!" } } }'; // $payload = '{ "message": { "token" : "cGAFgPJGf-s:APA91bF**...**aEVM17c9peqZ", "data":{ "val1" : " ", "val2" : "(Modern API) Firebase!", "val3" : " " } } }'; $send = ''; $send .= 'POST /v1/projects/pyur-test-id/messages:send HTTP/1.1'."\r\n"; $send .= 'Host: fcm.googleapis.com'."\r\n"; $send .= 'Connection: close'."\r\n"; $send .= 'Content-Type: application/json'."\r\n"; $send .= 'Authorization: Bearer '.$access_token."\r\n"; $send .= 'Content-Length: '.strlen($payload)."\r\n"; $send .= "\r\n"; $send .=$payload; $result = fwrite($socket, $send); $receive = ''; while (!feof($socket)) $receive .= fread($socket, 8192); fclose($socket); echo '<pre>'.$receive.'</pre>'; ?>
Source: https://habr.com/ru/post/345942/
All Articles