Pouvons-nous créer des services de repos pour l'envoi d'une notification à l'application Android sans utiliser la messagerie Cloud de Cloud Firebase/Google Cloud Messaging ou un autre outil de messagerie cloud? Si oui, comment cela est-il possible? J'ai cherché la même chose mais je n'ai pas pu obtenir le résultat approprié. Le résultat obtenu est que les services REST sont combinés avec l'outil cloud messenger, mais je souhaite créer des services grâce auxquels je pourrais facilement envoyer une notification sans console Cloud Messenger ouverte.Pouvons-nous envoyer une notification globalement sans utiliser firebase?
Code pour gérer les données de php
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);
// play notification sound
/* NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
notificationUtils.playNotificationSound();*/
}
}
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
} else {
// app is in background, show the notification in notification tray
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 {
// image is present, show notification with image
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());
}
}
Avec gcm ou fcm, vous n'avez pas besoin d'ouvrir la console pour envoyer des messages. Vous pouvez envoyer une requête http json à leur serveur et ils s'occuperont du reste. Voir la [documentation] (https://firebase.google.com/docs/cloud-messaging/http-server-ref) – RobCo
J'ai lu la documentation mais je n'ai toujours pas obtenu ma réponse –