在 Android 中单击 Firebase 通知时将数据发送到 activity
Send data to activity when Firebase notification is clicked in Android
在我的应用程序中,我想将 fireBase 用于 notification。
我想在点击通知时(当应用程序关闭时,我的意思是应用程序是 background
)用 putExtra
发送数据到 mainActivity.
我写了下面的代码,但是当点击 notification 时(在应用程序中是后台)但是显示 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);
}
}
主要活动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;
}
当点击通知时(在应用程序上是背景),在 Toast
中显示 null 消息 String value = getIntent().getStringExtra("fcm_notification");
我该如何解决?
从控制台发送通知时,添加来自 'Advanced options' 的自定义数据:
出于某种原因,firebase 不允许密钥以 fcm 开头。您不能使用 fcm_notification
。使用不同的密钥。
如上图,领取密钥如下:
String value = getIntent().getStringExtra("test_key");
试试这个:
Intent intent = new Intent(mCtx, MainActivity.class);
intent.putExtra("EXTRA_DATA", title);
并获得:
String value = getIntent().getStringExtra("EXTRA_DATA");
最好换个密钥。此外,您似乎需要从控制台发送数据,然后将数据发送到另一个 Activity
.
我已经使用当前通知 title
检查它是否 returns 标题或 not.If 它返回了值,所以,请尝试从控制台发送数据。
在我的应用程序中,我想将 fireBase 用于 notification。
我想在点击通知时(当应用程序关闭时,我的意思是应用程序是 background
)用 putExtra
发送数据到 mainActivity.
我写了下面的代码,但是当点击 notification 时(在应用程序中是后台)但是显示 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);
}
}
主要活动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;
}
当点击通知时(在应用程序上是背景),在 Toast
中显示 null 消息 String value = getIntent().getStringExtra("fcm_notification");
我该如何解决?
从控制台发送通知时,添加来自 'Advanced options' 的自定义数据:
出于某种原因,firebase 不允许密钥以 fcm 开头。您不能使用 fcm_notification
。使用不同的密钥。
如上图,领取密钥如下:
String value = getIntent().getStringExtra("test_key");
试试这个:
Intent intent = new Intent(mCtx, MainActivity.class);
intent.putExtra("EXTRA_DATA", title);
并获得:
String value = getIntent().getStringExtra("EXTRA_DATA");
最好换个密钥。此外,您似乎需要从控制台发送数据,然后将数据发送到另一个 Activity
.
我已经使用当前通知 title
检查它是否 returns 标题或 not.If 它返回了值,所以,请尝试从控制台发送数据。