【问题标题】:Firebase push notification sent twiceFirebase 推送通知发送两次
【发布时间】:2017-02-12 16:27:13
【问题描述】:

我正在开发具有 firebase 通知的应用程序,我按照 firebase 教程指南了解如何发送和接收通知,但我在其他设备上收到 2 个通知,而不是收到 1 个通知并从发送的设备,虽然该设备不应该收到从它发送的通知

我看到了有关两次上传通知的其他问题,但它对我不起作用

P.S:通知是从具有标题编辑文本和正文编辑文本的自定义对话框发送的

这是清单:

<service
        android:name=".MyFirebaseInstanceIDService"
        android:exported="false">
        <intent-filter>
            <action
                android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

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

我在 gradle 中编译这些:

compile 'com.google.firebase:firebase-core:10.0.0'
compile 'com.google.firebase:firebase-messaging:10.0.0'

这是自定义对话框的代码:

builder = new AlertDialog.Builder(this);
        final View view = View.inflate(this, R.layout.layout_send_notification, null);
        builder.setView(view)
                .setCancelable(true)
                .setPositiveButton(getString(R.string.send), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        EditText editMessage = (EditText) view.findViewById(R.id.notification_message);
                        EditText editTitle = (EditText) view.findViewById(R.id.notification_title);
                        Calendar now = Calendar.getInstance();
                        int day = now.get(Calendar.DAY_OF_MONTH);
                        int month = now.get(Calendar.MONTH) + 1;
                        int year = now.get(Calendar.YEAR);
                        String message = editMessage.getText().toString().replace(' ', '_');
                        String title = editTitle.getText().toString().replace(' ', '_');

                        boolean cancel = false;
                        View focusView = null;

                        if (TextUtils.isEmpty(message)) {
                            editMessage.setError(getString(R.string.error_field_required));
                            focusView = editMessage;
                            cancel = true;
                        }

                        if (TextUtils.isEmpty(title)) {
                            editTitle.setError(getString(R.string.error_field_required));
                            focusView = editTitle;
                            cancel = true;
                        }

                        if (cancel)
                            focusView.requestFocus();
                        else {
                            RequestQueue queue = Volley.newRequestQueue(NotificationsActivity.this);
                            String url = "https://mysite.here/send_push.php?" +
                                    "message=" + message +
                                    "&title=" + title +
                                    "&day=" + day +
                                    "&month=" + month +
                                    "&year=" + year;
                            StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    Log.d("send push response", response);
                                }
                            }, new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    error.printStackTrace();
                                }
                            });
                            queue.add(request);
                        }
                    }
                })
                .setNegativeButton(getString(R.string.button_cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.cancel();
                    }
                });
        sendNotificationDialog = builder.create();

这里是 firebaseMessagingService:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}

private void sendNotification(String messageTitle, String messageBody) {
    Intent intent = new Intent(this, SplashActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setSmallIcon(R.drawable.ic_stat_spe_bau)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

    notificationManager.notify(0 , notificationBuilder.build());
}

这里是 firebaseInstanceIDService:

@Override
public void onTokenRefresh() {
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d("firebase ID", refreshedToken);
    sendRegistrationToServer(refreshedToken);
}

private void sendRegistrationToServer(String token) {
    RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
    String url = "https://mysite.here/insertFCM_ID.php?fcmID=" + token;
    StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("fcm response", response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });
    queue.add(request);
}

send_push.php 代码:

<?php
header("Content-type: application/json, charset=UTF-8");
require('db.php');

$db = mysqli_connect($host, $username, $password) or die('Could not connect');
mysqli_select_db($db, $db_name) or die('');
$message    = $_GET['message'];
$title      = $_GET['title'];
$day        = $_GET['day'];
$month      = $_GET['month'];
$year       = $_GET['year'];

$server_key = "somekey";
$sql        = 'select fcm_token from fcm_info';
$result     = mysqli_query($db, $sql) or print("Error");
$key        = array();

while ($row = mysqli_fetch_assoc($result)) {
    $key[] = $row;
}

$headers = array(
    'Authorization: key=' . $server_key,
    'Content-Type: application/json'
);
$single  = "";

foreach($key as $value) {
    $single  = $value['fcm_token'];

    $fields  = array(
        'to' => $single,
        'notification' => array(
            'title' => str_replace("_", " ", $title),
            'body' => str_replace("_", " ", $message),
            'vibrate' => 1,
            'sound' => 1
        )
    );
    $payload = json_encode($fields);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    $result = curl_exec($ch);
}

mysqli_query($db, "INSERT INTO notifications (title, body, day, month, year) VALUES ('$title', '$message', '$day', '$month', '$year')");

mysqli_close($db);
echo $result;
?>

我尝试了很多方法来找出问题所在,但我不知道是什么

如果需要更多信息,我将重新编辑以添加所需信息

【问题讨论】:

  • 嗨。您可以发布您的send_push.php(删除敏感信息)吗?另外,从您的服务器发送消息后,您是否也收到了双重响应?
  • 您的意思是通知是否在我的数据库中注册了两次?如果是,那么是的
  • 嗨。没有。我指的是您从服务器发送消息,您收到 FCM 服务器的响应(可能是200 OK 响应),您是否也收到两次?
  • 嗯,我应该会收到回复,因为系统也是 iOS 平台,iOS 版本也收到 2 个通知

标签: android firebase notifications firebase-cloud-messaging


【解决方案1】:

在您的 onMessageReceived 事件中添加注释行:

//sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}

在您的 firebaseInstanceIDService 类中进行此更改后重试。应该没问题。 如果您只想使用下游消息,则需要确定您的消息类型。数据消息或通知。您还需要管理您的应用在设备上的 3 种情况:前台、后台和已终止。数据消息总是在您的服务上触发 onMessageReceived 事件。这在您发送通知消息时有所不同。我建议您将数据消息作为 Post 请求发送。你现在没有任何问题。您正在通过 Volley 向“https://fcm.googleapis.com/fcm/send”URL 发送 Post 请求,并且您正在发送通知。然后您的服务正在接收您的通知并创建另一个。收到 2 个通知是很正常的,因为您的代码正在生成 2 个通知。我告诉过你做评论并测试它。

【讨论】:

    【解决方案2】:

    您可以将 message-id 保存在一个变量中,然后当收到两次新消息时,第一个 message-id 将保存在此变量中,当它第二次调用两次时,您将检查变量中是否存在..例如

    String savedMessageId  = "";
    
     public void setupFCM() {
    
     if (savedMessageId != remoteMessage.messageId)
            savedMessageId  = remoteMessage.messageId;
          else
            return;
    
    
         // your code ...
      }
    

    【讨论】:

      猜你喜欢
      • 2019-03-18
      • 2018-02-26
      • 2019-11-11
      • 1970-01-01
      • 2022-07-16
      • 2022-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多