安卓系统:当应用程序在后台时.我想在单击通知时将静态值(1)传递给TotalRoomActivity活动



例如:关于问题的详细信息。。。当应用程序在后台时,我想在通知点击时向TotalRoomActivity传递一个值(1(。我已经实现了它,但TotalRoomActive中的数据显示为null。当应用程序打开时,它可以正常工作。但在后台时无法工作下面我提到了代码,请查找并检查。

MyFirebaseMessagingService:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static final String FCM_PARAM = "image";
private static final String CHANNEL_NAME = "FCM";
private static final String CHANNEL_DESC = "Firebase Cloud Messaging";
private int numMessages = 0;
String push_message="1";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
/*  MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.mindemo);
mediaPlayer.start();*/
RemoteMessage.Notification notification = remoteMessage.getNotification();
Map<String, String> data = remoteMessage.getData();
sendNotification(notification, data);
String click_action_data = remoteMessage.getData().get("click_action");
Intent intent = new Intent(click_action_data);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {
Bundle bundle = new Bundle();
bundle.putString(FCM_PARAM, data.get(FCM_PARAM));
Intent intent;
intent = new Intent(this, TotalRoomActivity.class);
intent.putExtra("push_message",push_message);
intent.putExtras(bundle);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, getString(R.string.notification_channel_id))
.setContentTitle(notification.getTitle())
.setContentText(notification.getBody())
.setAutoCancel(true)
.setSound(sound)
//.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.bubble))
.setContentIntent(pendingIntent)
.setContentInfo("Doale")
//.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.doaleblacklogo))
.setStyle(new NotificationCompat.BigTextStyle().bigText(notification.getBody()))
.setColor(getResources().getColor(R.color.colorPrimary))
.setLights(Color.RED, 1000, 300)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setNumber(++numMessages)
.setSmallIcon(R.drawable.doaleblacklogo);
Intent result_Intent = new Intent(getApplicationContext(), TotalRoomActivity.class);
result_Intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
try {
String picture = data.get(FCM_PARAM);
if (picture != null && !"".equals(picture)) {
URL url = new URL(picture);
Bitmap bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream());
notificationBuilder.setStyle(
new NotificationCompat.BigPictureStyle().bigPicture(bigPicture).setSummaryText(notification.getBody())
);
}
} catch (IOException e) {
e.printStackTrace();
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
getString(R.string.notification_channel_id), CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT
);
channel.setDescription(CHANNEL_DESC);
channel.setShowBadge(true);
channel.canShowBadge();
channel.enableLights(true);
channel.setLightColor(Color.RED);
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500});
assert notificationManager != null;
notificationManager.createNotificationChannel(channel);
}
assert notificationManager != null;
notificationManager.notify(0, notificationBuilder.build());
}

}

FirebaseBackgroundService:

公共类FirebaseBackgroundService扩展WakefulBroadcastReceiver{

private static final String TAG = "FirebaseService";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "I'm in!!!");
if (intent.getExtras() != null) {
for (String key : intent.getExtras().keySet()) {
Object value = intent.getExtras().get(key);
Log.e("FirebaseDataReceiver", "Key: " + key + " Value: " + value);
if (key.equalsIgnoreCase("gcm.notification.body") && value != null) {
Bundle bundle = new Bundle();
Intent backgroundIntent = new Intent(context, TotalRoomActivity.class);
backgroundIntent.putExtra("push_message", "1");
backgroundIntent.putExtras(bundle);
context.startService(backgroundIntent);
}
}
}
}

}

总房间活动:

public class TotalRoomActivity extends AppCompatActivity {
RecyclerView room_recycleview;
ArrayList<Integer> room_list = new ArrayList<>();
ArrayList<String> room_number_list = new ArrayList<>();
String getstatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_room_total);
if (getstatus==null){
getstatus="";
}
getstatus = getIntent().getStringExtra("push_message");
Toast.makeText(this, getstatus, Toast.LENGTH_SHORT).show();
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar.getNavigationIcon().setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
});
room_recycleview = findViewById(R.id.room_recycleview);
room_recycleview.setHasFixedSize(true);
room_recycleview.hasFixedSize();
GridLayoutManager gridLayoutManager = new GridLayoutManager(TotalRoomActivity.this, 2);
room_recycleview.setLayoutManager(gridLayoutManager);
ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(TotalRoomActivity.this, R.dimen.item_offset);
room_recycleview.addItemDecoration(itemDecoration);
room_recycleview.setNestedScrollingEnabled(false);

room_list.add(R.drawable.roomonee);
room_list.add(R.drawable.roomtwoo);
room_list.add(R.drawable.roomthreee);
room_list.add(R.drawable.roomfourr);
room_list.add(R.drawable.rooomfivee);
room_list.add(R.drawable.roomsixx);
room_number_list.add("R1");
room_number_list.add("R2");
room_number_list.add("R3");
room_number_list.add("R4");
room_number_list.add("R5");
room_number_list.add("R6");
RoomAdapter roomAdapter = new RoomAdapter(TotalRoomActivity.this, room_list,room_number_list);
room_recycleview.setAdapter(roomAdapter);
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left);
}

}

您是否尝试检查";on简历"TotalRoomActivity的方法?有时,如果活动是在后台启动的,如果您单击通知,onReceive方法不会激活,但应用程序仍会收集有关意图的信息。

相关内容

  • 没有找到相关文章

最新更新