在Android中单击Firebase通知时将数据发送到活动 [英] Send data to activity when Firebase notification is clicked in Android
问题描述
在我的应用程序中,我想使用 fireBase 进行通知.
我想在单击通知时(应用程序关闭时,我的意思是应用程序是background
)将putExtra
的数据发送到 mainActivity .
我写下面的代码,但是当单击通知(在应用程序中是后台)时,却显示空表示getStringExtra
!
MyNotificationManager类:
public class MyNotificationManager {
private Context mCtx;
private Uri soundUri;
private static MyNotificationManager mInstance;
public MyNotificationManager(Context context) {
mCtx = context;
}
public static synchronized MyNotificationManager getInstance(Context context) {
if (mInstance == null) {
mInstance = new MyNotificationManager(context);
}
return mInstance;
}
public void displayNotification(String title, String body) {
soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(mCtx, MainActivity.class);
intent.putExtra("fcm_notification", "Y");
PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx, Constants.NOTIF_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setSound(soundUri)
.setAutoCancel(true)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
.setContentText(body)
.setContentIntent(pendingIntent);
NotificationManager mNotifyMgr = (NotificationManager) mCtx.getSystemService(NOTIFICATION_SERVICE);
if (mNotifyMgr != null) {
mNotifyMgr.notify(1, mBuilder.build());
}
}
}
MyFirebaseMessagingService类:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
showNotify(remoteMessage.getFrom(), remoteMessage.getNotification().getBody());
}
private void showNotify(String title, String body) {
MyNotificationManager myNotificationManager = new MyNotificationManager(getApplicationContext());
//myNotificationManager.displayNotification(title, body);
myNotificationManager.displayNotification(title, body);
}
}
MainActivity类:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (checkIntent()) return;
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
checkIntent();
}
}, 1000);
}
private boolean checkIntent() {
String value = getIntent().getStringExtra("fcm_notification");
Toast.makeText(context, "" + value, Toast.LENGTH_SHORT).show();
if (value == null) return false;
if (value.equals("Y")) {
startActivity(new Intent(this, LandingActivity.class));
// open one activity.
} else if (value.equals("another thing")) {
// open another activity.
}
finish();
return true;
}
单击通知(在应用程序上为后台)时,在Toast
中向此行String value = getIntent().getStringExtra("fcm_notification");
我该如何解决?
从控制台发送通知时,请从高级选项"中添加自定义数据:
由于某些原因,firebase不允许密钥以fcm开头.您不能使用fcm_notification
.使用其他密钥.
对于以上图像,请按以下方式接收密钥:
String value = getIntent().getStringExtra("test_key");
In my application I want use fireBase for notification.
I want when click on notification (when app is closed, my mean is app is background
) send data with putExtra
to mainActivity.
I write below codes, but when click on notification (in app is background) but show me null for getStringExtra
!
MyNotificationManager class :
public class MyNotificationManager {
private Context mCtx;
private Uri soundUri;
private static MyNotificationManager mInstance;
public MyNotificationManager(Context context) {
mCtx = context;
}
public static synchronized MyNotificationManager getInstance(Context context) {
if (mInstance == null) {
mInstance = new MyNotificationManager(context);
}
return mInstance;
}
public void displayNotification(String title, String body) {
soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(mCtx, MainActivity.class);
intent.putExtra("fcm_notification", "Y");
PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx, Constants.NOTIF_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setSound(soundUri)
.setAutoCancel(true)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
.setContentText(body)
.setContentIntent(pendingIntent);
NotificationManager mNotifyMgr = (NotificationManager) mCtx.getSystemService(NOTIFICATION_SERVICE);
if (mNotifyMgr != null) {
mNotifyMgr.notify(1, mBuilder.build());
}
}
}
MyFirebaseMessagingService class :
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
showNotify(remoteMessage.getFrom(), remoteMessage.getNotification().getBody());
}
private void showNotify(String title, String body) {
MyNotificationManager myNotificationManager = new MyNotificationManager(getApplicationContext());
//myNotificationManager.displayNotification(title, body);
myNotificationManager.displayNotification(title, body);
}
}
MainActivity class :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (checkIntent()) return;
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
checkIntent();
}
}, 1000);
}
private boolean checkIntent() {
String value = getIntent().getStringExtra("fcm_notification");
Toast.makeText(context, "" + value, Toast.LENGTH_SHORT).show();
if (value == null) return false;
if (value.equals("Y")) {
startActivity(new Intent(this, LandingActivity.class));
// open one activity.
} else if (value.equals("another thing")) {
// open another activity.
}
finish();
return true;
}
When click on notification (on app is background) , show me null message in Toast
for this line String value = getIntent().getStringExtra("fcm_notification");
How can i fix it?
When sending notification from console, add the custom data from 'Advanced options':
For some reason, firebase doesn't allow the key to start with fcm. You cannot use fcm_notification
. Use a different key.
For above image, receive the key as follows:
String value = getIntent().getStringExtra("test_key");
这篇关于在Android中单击Firebase通知时将数据发送到活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!