[英]how to show the notification data in new activity using firebase in android studio?
Here is my code of notification. 这是我的通知代码。 Actually, I'm getting the notification perfectly but when I click on the notification in a phone the app will open directly (when the app is in the background or killed).
实际上,我可以完美地收到通知,但是当我在电话中单击通知时,该应用将直接打开(当该应用在后台或被杀死时)。 When the app is in the foreground, the
ResultActivity
will open correctly when I click on notification but the data is not showing over there. 当应用程序位于前台时,当我单击通知时
ResultActivity
将正确打开,但数据未显示在那上面。
Please check my code and let me know what I did wrong. 请检查我的代码,让我知道我做错了什么。
FirebaseMessagingService.java: FirebaseMessagingService.java:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
Map<String, String> data;
String Title, Message, PushType;
String body, title;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e(TAG, "From: " + remoteMessage.getFrom() + "");
data = remoteMessage.getData();
JSONObject jsonObject = new JSONObject(data);
try {
Log.e("data", jsonObject.toString());
body = jsonObject.getString("body");
title = jsonObject.getString("title");
Log.e("body", body);
Log.e("title", title);
} catch (JSONException e) {
Log.e("exception>>>", e.toString() + "");
e.printStackTrace();
body = remoteMessage.getNotification().getBody();
Log.e("body>>>", body + "");
}
sendNotification(body, title);
}
private void sendNotification(String message, String title){
Log.e("message>>>", message + "");
Intent intent = new Intent(this, ResultActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("body",message);
intent.putExtra("title",title);
int requestCode = 0;
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.fc)
//.setSmallIcon(getNotificationIcon())
.setContentText(message)
.setColor(ContextCompat.getColor(this, R.color.colorPrimary))
.setAutoCancel(true)
.setSound(sound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, noBuilder.build()); //0 = ID of notification
}
ResultActivity.java: ResultActivity.java:
public class ResultActivity extends Activity {
private TextView Title,Message ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_view);
Title=(TextView)findViewById(R.id.title);
Message=(TextView)findViewById(R.id.message);
if(getIntent().getExtras()!=null)
{
for(String key : getIntent().getExtras().keySet())
{
if(key.equals("title"))
Title.setText(getIntent().getExtras().getString(key));
else if(key.equals("message"))
Message.setText(getIntent().getExtras().getString(key));
}
}
}
}
activity_notification_view.xml: activity_notification_view.xml:
<TextView
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/title"
android:hint="title"
android:gravity="center" />
<TextView
android:hint="message"
android:layout_marginTop="20dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/message" />
Intent intent = new Intent(this, ResultActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("body",message);
intent.putExtra("title",title);
//in activity add //在活动中添加
String body =getIntent().getStringExtra("body");
String title =getIntent().getStringExtra("title");
You are using different key in putExtra and in getExtra. 您在putExtra和getExtra中使用了不同的键。
In above code you are passing data in body and title key and you are trying to find the data on title and message . 在上面的代码中,您正在正文和标题键中传递数据, 并且试图在标题和消息中查找数据。
Use same key for for getter and setter. 为getter和setter使用相同的键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.