简体   繁体   English

使用 Android 应用程序向 FCM 发送通知

[英]Send Notification to FCM Using Android App

I want to build client and admin android apps such that the admin app sends a notification to FCM (API) and the users of the client app get this notification.我想构建客户端和管理 android 应用程序,以便管理应用程序向 FCM (API) 发送通知,并且客户端应用程序的用户收到此通知。

So, I used the Firebase Admin SDK to push the notification from the admin app to FCM by using the FCM documentation but there's something strange in the next code (from the documentation)因此,我使用 Firebase Admin SDK 通过使用FCM 文档将通知从管理应用程序推送到 FCM,但下一个代码中有一些奇怪的东西(来自文档)

// This registration token comes from the client FCM SDKs.
String registrationToken = "YOUR_REGISTRATION_TOKEN";

// See documentation on defining a message payload.
Message message = Message.builder()
.setNotification(new Notification(
    "$GOOG up 1.43% on the day",
    "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day."))
.setCondition(condition)
.build();


// Send a message to the device corresponding to the provided
    // registration token.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
 System.out.println("Successfully sent message: " + response);

As the send(RemoteMessage) accepts RemoteMessage not a Message object so how could I modified the previous code to a send the notification using RemoteMessage object由于send(RemoteMessage)接受RemoteMessage而不是Message对象,所以我如何修改以前的代码以使用RemoteMessage对象发送通知

You seem to be trying to mix the Firebase Admin SDK, with the Firebase Cloud Messaging SDK for Android.您似乎正在尝试将 Firebase Admin SDK 与适用于 Android 的 Firebase Cloud Messaging SDK 混合使用。 This is not possible.这不可能。

Any process that uses the Admin SDK is granted full, unlimited access to your Firebase project.任何使用 Admin SDK 的进程都被授予对 Firebase 项目的完全、无限制的访问权限。 So if you were to put it in a client-side app, everyone with that app could send FCM messages to any of your users, but also: list all those users, delete your entire database, overwrite your Cloud Functions, etc. For this reason the Firebase Admin SDK should/can only be used in a trusted environment, such as your development machine, a server you control, or Cloud Functions.因此,如果您将其放在客户端应用程序中,那么拥有该应用程序的每个人都可以向您的任何用户发送 FCM 消息,而且还可以:列出所有这些用户、删除整个数据库、覆盖您的云函数等。为此Firebase Admin SDK 应该/只能在受信任的环境中使用的原因,例如您的开发机器、您控制的服务器或 Cloud Functions。

To send messages to a device through Firebase Cloud Messaging, you will always need to have a trusted environment, often referred to as an app server in the FCM documentation.要通过 Firebase Cloud Messaging设备发送消息,您始终需要有一个受信任的环境,在 FCM 文档中通常称为应用服务器

When you run the Admin SDK on that trusted environment, you can call the FirebaseMessaging.getInstance().send() method , which takes the Message returned by build() as its parameter.当您在该可信环境中运行 Admin SDK 时,您可以调用FirebaseMessaging.getInstance().send() 方法,该方法build()返回的Message作为其参数。

Also see:另见:

Use My code work like charms : sends a notification to a particular user and manages click of notification.使用我的代码像魅力一样工作:向特定用户发送通知并管理通知的点击。

implementation 'com.google.firebase:firebase-messaging:20.1.3'
implementation 'com.google.firebase:firebase-analytics:17.2.3'

Manifest显现

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="default_channel_id" />

    <service
        android:name=".FCMService" >
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

Activity活动

  1.  project setting > cloud messaging > server key
      AUTH_KEY = server key

  2.  token id of a particular device is 
      String device_token = FirebaseInstanceId.getInstance().getToken();

When sending notification发送通知时

  new Thread(new Runnable() {
        @Override
        public void run() {
            pushNotification(senderName + " : " + message);

        }
    }).start();

  private void pushNotification(String message) {
    JSONObject jPayload = new JSONObject();
    JSONObject jNotification = new JSONObject();
    JSONObject jData = new JSONObject();
        try {
            // notification can not put when app is in background
            jNotification.put("title", getString(R.string.app_name));
            jNotification.put("body", message);
            jNotification.put("sound", "default");
            jNotification.put("badge", "1");
            jNotification.put("icon", "ic_notification");

//            jData.put("picture", "https://miro.medium.com/max/1400/1*QyVPcBbT_jENl8TGblk52w.png");

            //to token of any deivce
            jPayload.put("to", device_token);

            // data can put when app is in background
            jData.put("goto_which", "chatactivity");
            jData.put("user_id", mCurrentUserId);

            jPayload.put("priority", "high");
            jPayload.put("notification", jNotification);
            jPayload.put("data", jData);

            URL url = new URL("https://fcm.googleapis.com/fcm/send");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Authorization", "key=" + AUTH_KEY);
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setDoOutput(true);

            // Send FCM message content.
            OutputStream outputStream = conn.getOutputStream();
            outputStream.write(jPayload.toString().getBytes());

            // Read FCM response.
            InputStream inputStream = conn.getInputStream();
            final String resp = convertStreamToString(inputStream);

        } catch (JSONException | IOException e) {
            e.printStackTrace();
        }
    }

    private String convertStreamToString(InputStream is) {
        Scanner s = new Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next().replace(",", ",\n") : "";
    }

Add service Class添加服务类

    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.media.RingtoneManager;
    import android.net.Uri;
    import android.util.Log;

    import androidx.annotation.NonNull;
    import androidx.core.app.NotificationCompat;

    import com.google.android.gms.tasks.OnCompleteListener;
    import com.google.android.gms.tasks.Task;
    import com.google.firebase.iid.FirebaseInstanceId;
    import com.google.firebase.iid.InstanceIdResult;
    import com.google.firebase.messaging.FirebaseMessagingService;
    import com.google.firebase.messaging.RemoteMessage;

    public class FCMService extends FirebaseMessagingService {
        String goto_which, user_id;

        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            super.onMessageReceived(remoteMessage);
            Log.d("ff", "messddda recice: " + remoteMessage.getNotification().getTicker());
            String title = remoteMessage.getNotification().getTitle();
            String body = remoteMessage.getNotification().getBody();

            if (remoteMessage.getData().size() > 0) {
                goto_which = remoteMessage.getData().get("goto_which").toString();
                user_id = remoteMessage.getData().get("user_id").toString();
                Log.d("ff", "messddda send: " + goto_which);
            }
            sendnotification(title, body, goto_which, user_id);
        }

        private void sendnotification(String title, String body, String goto_which, String user_id) {
            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra("goto_which", goto_which);
            intent.putExtra("user_id", user_id);

            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
            Uri defaultsounduri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder nbuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setAutoCancel(true)
                    .setSound(defaultsounduri)
                    .setContentIntent(pendingIntent);

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, nbuilder.build());


            FirebaseInstanceId.getInstance().getInstanceId()
                    .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
                        @Override
                        public void onComplete(@NonNull Task<InstanceIdResult> task) {
                            if (!task.isSuccessful()) {
                                Log.w("f", "getInstanceId failed", task.getException());
                                return;
                            }

                            // Get new Instance ID token
                            String token = task.getResult().getToken();

                        }
                    });

        }
    }

Manage click of notification :管理点击通知:

It calls launcher activity so in the launcher activity you can manage call of a particular activity它调用启动器活动,因此在启动器活动中您可以管理特定活动的调用

Your launcher activity looks like您的启动器活动看起来像

 if (getIntent().getExtras() != null) {
        String goto_which = getIntent().getStringExtra("goto_which");
        String chatUser = getIntent().getStringExtra("user_id");

        if (goto_which.equals("chatactivity")) {
            Intent chatIntent = new Intent(getBaseContext(), ChatActivity.class);
            chatIntent.putExtra("user_id", chatUser);
            startActivity(chatIntent);
        } else if (goto_which.equals("grpchatactivity")) {
            Intent chatIntent = new Intent(getBaseContext(), GroupChatActivity.class);
            chatIntent.putExtra("group_id", chatUser);
            startActivity(chatIntent);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM