Java 在Android中单击Firebase通知时向活动发送数据
在我的应用程序中,我希望使用fireBase进行通知Java 在Android中单击Firebase通知时向活动发送数据,java,android,firebase,android-intent,android-notifications,Java,Android,Firebase,Android Intent,Android Notifications,在我的应用程序中,我希望使用fireBase进行通知 我想在单击通知时(当应用程序关闭时,我的意思是应用程序是后台)使用putExtra将数据发送到mainActivity 我写了下面的代码,但是当点击通知时(应用程序中是后台),但是为getStringExtra显示null MyNotificationManager类: public class MyNotificationManager { private Context mCtx; private Uri soundUr
我想在单击通知时(当应用程序关闭时,我的意思是应用程序是
后台
)使用putExtra
将数据发送到mainActivity我写了下面的代码,但是当点击通知时(应用程序中是后台),但是为
getStringExtra
显示nullMyNotificationManager类:
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());
}
}
}
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);
}
}
MyFirebaseMessagingService类:
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());
}
}
}
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);
}
}
main活动类:
@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;
}
Intent intent = new Intent(mCtx, MainActivity.class);
intent.putExtra("EXTRA_DATA", title);
当点击通知(在应用程序上为后台)时,在Toast
中为此行显示null消息String value=getIntent().getStringExtra(“fcm_通知”)代码>
我怎样才能修好它 从控制台发送通知时,从“高级选项”添加自定义数据:
由于某些原因,firebase不允许密钥以fcm开头。您不能使用fcm\U通知。使用不同的键
对于上图,按如下方式接收按键:
String value = getIntent().getStringExtra("test_key");
试试这个:
@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;
}
Intent intent = new Intent(mCtx, MainActivity.class);
intent.putExtra("EXTRA_DATA", title);
并获得:
String value = getIntent().getStringExtra("EXTRA_DATA");
最好换一下钥匙。另外,您似乎需要从控制台发送数据,然后将数据发送到另一个活动
我已使用当前通知title
检查它是否返回了标题。如果它返回了值,请尝试从控制台发送数据。当应用程序处于后台时,您需要发送数据负载
以从通知中获取数据。@Yupi,您能帮助我吗?请将以上代码发送给我。你能帮我吗?你如何发送通知?从Firebase控制台?再次显示空:(:(请帮帮我,我真的需要你的帮助,我的兄弟。这很奇怪。你是否从Firebase控制台添加了自定义数据?你是否用你使用的密钥替换了fcm_通知
?如果应用程序在后台,方法onMessageReceived
中的代码无关紧要。通知是由库内部创建的。只要在发送通知之前,从FCM控制台添加自定义数据。我使用下面的新代码更改代码。请参见。在MyNotificationManager=Intent Intent Intent=new Intent(mCtx,MainActivity.class);Intent.putExtra(“test_key”,keyTest);PendingIntent PendingIntent=PendingIntent.getActivity(mCtx,0,intent,PENDINGENT.FLAG_UPDATE_CURRENT)
---在MyFirebaseMessagingService=showNotify(remoteMessage.getFrom()、remoteMessage.getNotification().getBody()、remoteMessage.getData().get(“测试密钥”);
---并在fireBase控制台上的自定义数据中添加test_密钥
。再次显示空值。