我正在尝试为Firebase通知制作头条消息(例如Massenger
)
但是我的问题是应用启动时会直接显示该消息 没有收到任何消息。
我认为第一个问题的原因是我正在oncreate
中校准代码,但是除非发送了消息,否则我不会添加视图。
这是代码
public class FireBaseMessagingService extends FirebaseMessagingService {
FirebaseAuth mAuth;
private WindowManager mWindowManager;
private View mFloatingView;
private CircleImageView mClose;
private CircleImageView mImage;
WindowManager.LayoutParams params;
DatabaseReference mFriendDb;
String myId = "";
String FromId = "";
boolean onCreate = true;
@Override
public void onCreate() {
super.onCreate();
//Inflate the floating view layout we created
mFloatingView = LayoutInflater.from(this).inflate(R.layout.message_head, null);
mAuth = FirebaseAuth.getInstance();
myId = mAuth.getUid();
mFriendDb = FirebaseDatabase.getInstance().getReference();
mFriendDb.keepSynced(true);
//Add the view to the window
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mClose = mFloatingView.findViewById(R.id.iv_message_head_close);
mImage = mFloatingView.findViewById(R.id.iv_message_head_image);
if (onCreate == false || !FromId.equals(myId))
{
addingView();
}
mClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
closeImage();
}
});
}
和onMessageRecieve
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
onCreate = false;
Map<String, String> data = remoteMessage.getData();
FromId = data.get("From");
String MessageType = data.get("Type");
Log.i("MessageFrom" , "" + FromId);
Log.i("MessageType","" + MessageType);
mFriendDb.child(App_Constants.USERS_CELL).child(FromId).child(App_Constants.USER_INFO_CELL);
mFriendDb.keepSynced(true);
if (!FromId.equals(myId))
{
if (MessageType.equals(App_Constants.SENT_CELL))
{
mFriendDb.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
final String Image = dataSnapshot.child(App_Constants.USER_IMAGE_CELL).getValue().toString();
Picasso.get().load(Image).networkPolicy(NetworkPolicy.OFFLINE).into(mImage, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError(Exception e) {
Picasso.get().load(Image).into(mImage);
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
//Add the view to the window.
}
和addingview方法
private void addingView() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
}else{
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
}
//Specify the view position
params.gravity = Gravity.TOP | Gravity.LEFT; //Initially view will be added to top-left corner
params.x = 0;
params.y = 100;
ringTone();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if (!Settings.canDrawOverlays(this))
{
Intent myIntent = new Intent(this, InvisibleActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(myIntent);
}
else
{
mWindowManager.addView(mFloatingView, params);
}
}
}
答案 0 :(得分:0)
我通过首先从oncreate中删除代码来解决了它 并在onMessageReceived方法中创建一个Handler 这是完整的代码
public class FireBaseMessagingService extends FirebaseMessagingService {
FirebaseAuth mAuth;
private WindowManager mWindowManager;
private View mFloatingView;
private CircleImageView mClose;
private CircleImageView mImage;
WindowManager.LayoutParams params;
DatabaseReference mFriendDb;
String myId = "";
String FromId = "";
@Override
public void onCreate() {
super.onCreate();
//Inflate the floating view layout we created
mAuth = FirebaseAuth.getInstance();
myId = mAuth.getUid();
mFriendDb = FirebaseDatabase.getInstance().getReference();
mFriendDb.keepSynced(true);
//Add the view to the window
mFloatingView = LayoutInflater.from(this).inflate(R.layout.message_head, null);
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mClose = mFloatingView.findViewById(R.id.iv_message_head_close);
mImage = mFloatingView.findViewById(R.id.iv_message_head_image);
mClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
closeImage();
}
});
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Map<String, String> data = remoteMessage.getData();
FromId = data.get("From");
String MessageType = data.get("Type");
Log.i("MessageFrom" , "" + FromId);
Log.i("MessageType","" + MessageType);
if (!FromId.equals(myId))
{
if (MessageType.equals(App_Constants.SENT_CELL))
{
mFriendDb.child(App_Constants.USERS_CELL).child(FromId).child(App_Constants.USER_INFO_CELL).
addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
final String Image = dataSnapshot.child(App_Constants.USER_IMAGE_CELL).getValue().toString();
if (Image != null)
{
Picasso.get().load(Image).networkPolicy(NetworkPolicy.OFFLINE).into(mImage, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError(Exception e) {
Picasso.get().load(Image).into(mImage);
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
addingView();
}
});
}
//Add the view to the window.
}
private void addingView() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
}else{
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
}
//Specify the view position
params.gravity = Gravity.TOP | Gravity.LEFT; //Initially view will be added to top-left corner
params.x = 0;
params.y = 100;
ringTone();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if (!Settings.canDrawOverlays(this))
{
Log.i("IsGoing","Goind");
Intent myIntent = new Intent(this, InvisibleActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(myIntent);
}
else
{
mWindowManager.addView(mFloatingView, params);
}
}
}
private void closeImage()
{
if (mFloatingView != null) mWindowManager.removeView(mFloatingView);
}
private void ringTone(){
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
}
}