我想在获取特定(我检查通知正文内容)通知时启动一个活动,当我的应用程序处于前台,后台和被杀死状态时,我的代码正在运行。但是当我的手机被锁定时无法工作。在手机被锁定但通知被看到时,通知正文内容会在空白中显示为空白。
MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
Log.d(TAG, "FCM Data: " + remoteMessage.getData());
str = remoteMessage.getData().get("body");
StringBuilder myNumbers = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
if (Character.isDigit(str.charAt(i))) {
myNumbers.append(str.charAt(i));
System.out.println(str.charAt(i) + " is a digit.");
} else {
System.out.println(str.charAt(i) + " not a digit.");
}
}
System.out.println("Your numbers: " + myNumbers.toString());
str.replaceAll("\\D+", "");`enter code here`
String[] splited = str.split("\\s+");
FirstWord1 = splited[0];
String FirstWord = splited[1];
System.out.println("FW Word : " + FirstWord);
System.out.println("0 Word : " + FirstWord1);
//String FirstWord = splited[0];
ticketNo = myNumbers.toString();
GlobalVeriable.msg = FirstWord;
GlobalVeriable.ticketNo = ticketNo;
GlobalVeriable.showPopupStr = str;
GlobalVeriable.showPopupFirstword = FirstWord1;
GlobalVeriable.showMyNo = myNumbers.toString();
if (FirstWord1.equals("Please")) {
Intent intent = new Intent(this, OverlayActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else {
Intent intent = new Intent(click_action);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
String channelId = "Default";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setSound(sound)
// .setContentTitle(remoteMessage.getNotification().getTitle())
// .setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true)
.setContentIntent(pendingIntent);
;
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
manager.notify(0, builder.build());
}
@Override
public void onCreate() {
super.onCreate();
Intent intent = new Intent(this, OverlayActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("firstWord", FirstWord1);
startActivity(intent);
}
}
OverlayActivity.java
public class OverlayActivity extends Activity {
private static final String TAG = OverlayActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "[onCreate]");
super.onCreate(savedInstanceState);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Fragment fragment = getFragmentManager().findFragmentByTag(FragmentType.OVERLAY.getTag());
if (fragment == null) {
fragment = OverlayFragment.newInstance();
}
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(android.R.id.content, fragment, FragmentType.OVERLAY.getTag());
ft.commit();
}
private enum FragmentType {
OVERLAY("overlay");
private String tag;
private FragmentType(String tag) {
this.tag = tag;
}
public String getTag() {
return tag;
}
}
}
OverlayFragment.java
public class OverlayFragment extends Fragment {
MediaPlayer mp=new MediaPlayer();
public static OverlayFragment newInstance() {
OverlayFragment fragment = new OverlayFragment();
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_overlay, container, false);
Button close = (Button) view.findViewById(R.id.close);
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().finish();
}
});
// GlobalVeriable.showPopupFirstword="Please";
// GlobalVeriable.showPopupFirstword= getArguments().getString("firstWord");
Toast.makeText(getActivity(),GlobalVeriable.showPopupFirstword,Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(),"msg"+GlobalVeriable.msg,Toast.LENGTH_LONG).show();
if (GlobalVeriable.showPopupFirstword.equals("Please") /*|| GlobalVeriable.showPopupFirstword.equals("")*/){
//
mp = MediaPlayer.create(getActivity(),R.raw.ring);
mp.start();
final android.app.AlertDialog.Builder builder=new android.app.AlertDialog.Builder(getActivity());
builder.setTitle(" Vistor Request ");
builder.setMessage(GlobalVeriable.showPopupStr);
builder.setCancelable(false);
builder.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mp.stop();
GlobalVeriable.notiMsg="In";
//sendOwnerResponce();
}
});//builder.show();
builder.setNegativeButton("Reject", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mp.stop();
GlobalVeriable.notiMsg="Out";
//sendOwnerResponce();
}
});
// Dialog
final android.app.AlertDialog dialog = builder.create();
dialog.show();
dialog.getButton(dialog.BUTTON_POSITIVE).setTextColor(Color.parseColor("#48A21D"));
dialog.getButton(dialog.BUTTON_NEGATIVE).setTextColor(Color.RED);
new CountDownTimer(15000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
//dialog.setMessage("00:"+ (millisUntilFinished/1000));
}
@Override
public void onFinish() {
//info.setVisibility(View.GONE);
dialog.dismiss();
mp.stop();
getActivity().finish();
//callStatus(AppSharedPreference.getID(),mobile,flat);
}
}.start();
//mp.stop();
}
else {
getActivity().finish();
}
return view;
}
}
我怀疑FirebaseMessagingService的工作方式是这样的,我建议您编写自己的服务并使其在前台工作并在那里处理您的代码。