将FCM令牌存储到远程MySQL数据库

时间:2018-11-09 20:46:39

标签: android mysql firebase-cloud-messaging android-volley

我的应用程序能够在本地主机中存储注册令牌,但是无法将注册令牌存储在在线远程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(mRegistrationBroadcastReciever);     super.onPause();          }

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

LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReciever,new IntentFilter(“ registration Complete”));

LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReciever,new IntentFilter(Config.STR_PUSH));          }

    }

0 个答案:

没有答案