我正在使用FCM并也收到通知,即试图获取某人的经度和纬度,而无论他/她在没有进入android的应用程序的情况下向他或她获取通知,但在经度和纬度中却得到0.0
我在MainActivity中没有做任何事情,因为在后台获取位置并将其发送到服务器
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
private PendingIntent pendingIntent;
private String msg;
private String userEmail;
private String getMessageId;
private String getMessageType;
private String Mmobile;
private String msgTitle;
private Bitmap bitmap=null;
private String url;
private int sender_id;
private NotificationCompat.BigPictureStyle notiStyle;
private NotificationCompat.Builder notificationBuilder;
NotificationManager notificationManager;
Double latitude, longtude;
private LatLng latLng;
private LocationRequest mLocationRequest;
float googleApiAccuaracy;
private GoogleApiClient mGoogleApiClient;
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
try {
JSONObject jsonObject = new JSONObject(remoteMessage.getData());
/* url=jsonObject.getString("image");
Log.d("url ",url);*/
msgTitle=jsonObject.getString("title");
msg = jsonObject.getString("body");
sender_id=jsonObject.getInt("senderId");
/* if (url != null || url !=""){
bitmap = getBitmapfromUrl(url);
}*/
sendNotification(msg,bitmap,msgTitle);
String FCM_ID = AppPreferences.getInstance(MyFirebaseMessagingService.this).getString(AppPreferences.FCM_REG_ID,
null);
location();
// CallLoginService(FCM_ID, String.valueOf(sender_id));
/* DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
databaseAccess.open();
String date=getDateTime();
long status = databaseAccess.insertData(msgTitle,msg,url,date);
Log.d("Status",status+"");
databaseAccess.close();
if (status != -1) {
UserNotification.notify(Constants.NOTIFICATION_REGRESH, "");
} else {
}*/
} catch (JSONException e) {
e.printStackTrace();
}
}
}
/*
*To get a Bitmap image from the URL received
* */
public Bitmap getBitmapfromUrl(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private void sendNotification(String messageBody, Bitmap image, String title) {
notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder = new NotificationCompat.Builder(this, channelId);
Intent intent = new Intent(this, NotificationListActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, 0 , intent, PendingIntent.FLAG_ONE_SHOT);
// Bitmap tempBMP = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
//notificationBuilder.setLargeIcon(tempBMP);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(messageBody);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(defaultSoundUri);
if (image !=null){
notiStyle = new NotificationCompat.BigPictureStyle();
notiStyle.bigPicture(image);
notificationBuilder.setStyle(notiStyle);
}else {
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText(messageBody);
notificationBuilder.setStyle(bigText);
}
notificationBuilder.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription(messageBody);
channel.enableLights(true);
channel.setLockscreenVisibility(111);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(sender_id , notificationBuilder.build());
}
/**
* CallLoginService
*/
public void CallLoginService(String registrationId,String senderId) {
Map<String, String> headers = new HashMap<>();
try {
headers.put("registrationId", registrationId);
headers.put("senderId", senderId);
//Make request to server
new NotificationVollyPost().notificationVolleyPost(ServerUrls.NOTIFICATION_CHECKING,
new JSONObject(headers).toString(),
Constants.NOTIFICATION_PUSH_CHEK, this, null);
} catch (Throwable e) {
e.printStackTrace();
}
}
private String getDateTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
String outputPattern = "dd-MMM-yyyy h:mm a";
Date date = new Date();
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
String str = null;
str = outputFormat.format(date);
Log.d("date",str);
return str;
}
private void location(){
GPSTracker gp = new GPSTracker(this);
if (Utilities.isGpsEnable(this, gp)) {
if (latitude == null) {
latitude = gp.getLatitude();
longtude = gp.getLongitude();
System.out.println("location"+latitude+" "+longtude);
}
} else gp.showSettingsAlert();
}
}