简体   繁体   English

无法使用firebase云消息传递在mysql数据库中注册令牌

[英]Unable to register token in mysql database, using firebase cloud messaging

I was working a little with push notifications, which I managed to put working so far and with the Firebase Cloud Messaging Api, now I was trying to save the Token in the database, was following a tutorial and the token doesn't seem to register. 我正在使用推送通知进行一些工作,到目前为止我设法使用Firebase云消息传递Api,现在我试图将令牌保存在数据库中,遵循教程并且令牌似乎没有注册。

MainActivity.java http://prntscr.com/bgadg6 MainActivity.java http://prntscr.com/bgadg6

MyFirebaseInstanceIDService.java http://prntscr.com/bgadxd MyFirebaseInstanceIDService.java http://prntscr.com/bgadxd

MyFirebaseMessagingService extends Firebase Messaging Service{ MyFirebaseMessagingService扩展Firebase消息传递服务{

  private static final String TAG = "MyFirebaseMsgService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    //Displaying data in log
    //It is optional
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

    //Calling method to generate notification
    sendNotification(remoteMessage.getNotification().getBody());
}

//This method is only generating push notification
//It is same as we did in earlier posts
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, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Firebase Push Notification")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

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

} }

AndroidManifest.xml AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>

<!-- Adding Internet Permission -->

<uses-permission android:name="android.permission.INTERNET"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!--
        Defining Services
    -->
    <service
        android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>

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

Try this, with volley library & asyntask: 试试这个,使用volley库和asyntask:

//Creating a string request
        StringRequest req = new StringRequest(Request.Method.POST, Constants.REGISTER_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        //dismissing the progress dialog
                        progressDialog.dismiss();

                        //if the server returned the string success
                        if (response.trim().equalsIgnoreCase("success")) {
                            //Displaying a success toast
                            Toast.makeText(MainActivity.this, "Registered successfully", Toast.LENGTH_SHORT).show();


                        } else {
                            Toast.makeText(MainActivity.this, "Choose a different email", Toast.LENGTH_SHORT).show();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                //adding parameters to post request as we need to send firebase id and email
                params.put("firebaseid", uniqueId);
                params.put("email", email);
                return params;
            }

and register.php 和register.php

<?php 

if($_SERVER['REQUEST_METHOD']=='POST'){

    //getting the request values 
    $firebaseid = $_POST['firebaseid'];
    $email  = $_POST['email'];

    //connecting to database 
    $con = mysqli_connect('localhost','root','','pushnotification');

    //creating an sql query 
    $sql = "INSERT INTO register (firebaseid, email) VALUES ('$firebaseid','$email')";

    //executing the query to the database 
    if(mysqli_query($con,$sql)){
        echo 'success';
    }else{
        echo 'failure';
    }
}

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

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