0

I want to send notifications to topics with FCM without using the Firebase console. When I build a new message with the help of Firebase Documentation it´s a problem.

Picture of the problem

Here my Codes:

 public void sendToTopic() {

        Message message = Message.builder()
                .putData("score", "850")
                .putData("time", "2:45")
                .setTopic("1")
                .build();


    }

public class MyFirebaseMessagingService extends FirebaseMessagingService {


    private static final String TAG = "MyFirebaseMsgService";
    public void onMessageReceived(RemoteMessage remoteMessage) {

    }


    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String channelId = ("asda");
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.book_icon)
                        .setContentTitle("Test")
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

}

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
user
  • 9
  • 3
  • It is not possible to directly send a message to other devices from within your Android app. Sending so-called downstream messages requires that you specify the FCM server key, which (as its name implies) should only be used in trusted environments. So you'll need to send the messages from a server, or for example Cloud Functions. For a longer answer, see https://stackoverflow.com/a/37993724 – Frank van Puffelen Feb 09 '20 at 15:46
  • Thank you. But how can I use something like this: `code` https://fcm.googleapis.com/fcm/notification Content-Type:application/json Authorization:key=API_KEY project_id:SENDER_ID { "operation": "create", "notification_key_name": "appUser-Chris", "registration_ids": ["4", "8", "15", "16", "23", "42"] } How can I send a some JSON Data to Firebase? – user Feb 09 '20 at 16:43
  • That code needs to run on a server, as the `API_KEY` you use in the `Authorization` header is a secret. The answer I linked shows the architecture on how to accomplish this, and has a link to a tutorial I wrote a few years ago. – Frank van Puffelen Feb 09 '20 at 18:11

1 Answers1

1

The Message class is inside the Firebase admin sdk but you cannot use that in your android project, you can only use firebase admin sdk in the server side and there you will be able to use the Message class. Check the docs for reference:-

https://firebase.google.com/docs/cloud-messaging/manage-topics

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134