Android 获取活动中的通知消息
我允许用户点击通知,然后启动应用程序并打开特定活动 在我正在打开的活动中,我希望得到通过通知发送的消息 发送通知。。。方法: AnotherActivity.java: 更新1 发送通知。。。{ 在onCreate of AnotherActivity.java中尝试了这两种方法,但未收到消息Android 获取活动中的通知消息,android,firebase,android-intent,push-notification,Android,Firebase,Android Intent,Push Notification,我允许用户点击通知,然后启动应用程序并打开特定活动 在我正在打开的活动中,我希望得到通过通知发送的消息 发送通知。。。方法: AnotherActivity.java: 更新1 发送通知。。。{ 在onCreate of AnotherActivity.java中尝试了这两种方法,但未收到消息 String message=getIntent().getStringExtra("message"); Toast.makeText(YouTubeActivity.t
String message=getIntent().getStringExtra("message");
Toast.makeText(YouTubeActivity.this, message, Toast.LENGTH_LONG).show();
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("message");
Toast.makeText(YouTubeActivity.this, value, Toast.LENGTH_LONG).show();
//The key argument here must match that used in the other activity
}
只要这样做:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("AnotherActivity", TrueOrFalse);
intent.putExtra("message", messageBody);
因此,您的代码如下所示:
private void sendNotification(String messageTitle, String messageBody, Bitmap image, String TrueOrFalse) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("AnotherActivity", TrueOrFalse);
intent.putExtra("message", messageBody);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(image)/*Notification icon image*/
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image))/*Notification with Image*/
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
要获取数据,请执行以下操作:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
// here I would like to get value of messageBody
String message=getIntent().getStringExtra("message");
}
当你点击通知启动MainActivity或AnotherActivity?AnotherActivity.java好的我现在知道了,我必须调用AnotherActivity.java来代替MainActivity.jav让我试试这一个……过去两天我太笨了,我在想为什么我没有收到消息……无论如何,先让我试试看,你可以得到this intent.putextraonotheractivity,TrueOrFalse;?完成…我只是用AnotherActivity.java代替MainActivity.java…谢谢
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("AnotherActivity", TrueOrFalse);
intent.putExtra("message", messageBody);
private void sendNotification(String messageTitle, String messageBody, Bitmap image, String TrueOrFalse) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("AnotherActivity", TrueOrFalse);
intent.putExtra("message", messageBody);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(image)/*Notification icon image*/
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image))/*Notification with Image*/
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
// here I would like to get value of messageBody
String message=getIntent().getStringExtra("message");
}