将fcm令牌存储到远程mysql数据库

vlurs2pr  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(205)

我的应用程序能够在localhost中存储注册令牌,但无法在在线远程mysql数据库中存储注册令牌。
令牌首先保存在sharedpreferences中,然后存储到数据库服务器,但它只在本地计算机中工作,每次用户安装应用程序时,如何将其存储到远程数据库服务器中。

public class MainActivity extends AppCompatActivity {

private WebView webView;
private ProgressDialog dialog;
private BroadcastReceiver mRegistrationBroadcastReciever;
private final String CHANNEL_ID="notificcation";
String app_server_url="xxxxxxxxxxxxxxxxxxxxxxxx";

@Override
public void onBackPressed() {
AlertDialog.Builder builder=new AlertDialog.Builder(this);

if(webView.canGoBack()){

    webView.goBack();
}else{
    builder.setTitle("Do you want to leave the TuneMe App?");
    builder.setMessage("Are you sure?");

    builder.setPositiveButton("Exit", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });

    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    AlertDialog dialog=builder.show();
    }

     }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

SharedPreferences sharedPreferences=getApplicationContext().getSharedPreferences(getString(R.string.FCM_PREF), Context.MODE_PRIVATE);
final String token=sharedPreferences.getString(getString(R.string.FCM_TOKEN),"");

StringRequest stringRequest=new StringRequest(Request.Method.POST, app_server_url, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {

    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

    }
})

{
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String,String>params=new HashMap<String,String>();
        params.put("fcm_token",token);
        return params;
    }
};
MySingleton.getmInstance(MainActivity.this).addToRequestque(stringRequest);

webView=(WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("default_website");
webView.setWebViewClient(new WebViewClient(){

    @Override
    public void onPageFinished(WebView view, String url) {

        if(dialog.isShowing())
            dialog.dismiss();
    }
});

mRegistrationBroadcastReciever=new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent.getAction().equals(Config.STR_PUSH))
        {

            String message=intent.getStringExtra(Config.STR_MESSAGE);
            showNotification("TuneMe Article",message);

        }

    }

};

    onNewIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
    dialog=new ProgressDialog(this);
    if(intent.getStringExtra(Config.STR_KEY)!=null)
{
    dialog.show();
    dialog.setMessage("Please Wait");
    webView.loadUrl(intent.getStringExtra(Config.STR_KEY));

}
     }

      private void showNotification(String title, String message) {
      notificationChannel();
      Bitmap picture = 
      BitmapFactory.decodeResource(getResources(),R.mipmap.app_launcher);

    Intent intent =new Intent(getBaseContext(),MainActivity.class);
    intent.putExtra(Config.STR_KEY,message);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent=PendingIntent.getActivity(getBaseContext(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder=new NotificationCompat.Builder(getBaseContext(),CHANNEL_ID);
builder.setAutoCancel(true)
        .setWhen(System.currentTimeMillis())
        .setDefaults(Notification.DEFAULT_ALL)
        .setSmallIcon(R.drawable.ic_sms_black_24dp)
        .setContentTitle(title)
        .setContentText(message)
        .setLargeIcon(picture)
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setContentIntent(contentIntent);

NotificationManager notificationManager = (NotificationManager)getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,builder.build());

   }

   private void notificationChannel (){

   if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
   {
    CharSequence name="Personal Notificaiton";
    String description="include";
    int importance=NotificationManager.IMPORTANCE_DEFAULT;

    NotificationChannel notificationChannel=new NotificationChannel(CHANNEL_ID,name,importance);
    notificationChannel.setDescription(description);
    NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    notificationManager.createNotificationChannel(notificationChannel);

}

 }

    @Override
    protected void onPause() {

localbroadcastmanager.getinstance(this).unregisterreceiver(mrRegistrationBroadcastReceiver);super.onpause();}

@Override
      protected void onResume() {
      super.onResume();

localbroadcastmanager.getinstance(this).registerreceiver(mrRegistrationBroadcastReceiver,new intentfilter(“注册完成”));
localbroadcastmanager.getinstance(this).registerreceiver(mregistrationBroadcastReceiver,new intentfilter(config.str\u push));}

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题