这是我的通知代码。事实上,我收到的通知很完美,但当我点击手机中的通知时,应用程序会直接打开(当应用程序在后台或被关闭时(。当应用程序在前台时,当我点击通知时,ResultActivity
将正确打开,但数据没有显示在那里。
请检查我的代码,让我知道我做错了什么。
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:
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:
<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);
//在活动中添加
String body =getIntent().getStringExtra("body");
String title =getIntent().getStringExtra("title");
您在putExtra和getExtra中使用不同的键。
在上面的代码中,您正在传递正文和标题键中的数据,并试图查找标题和消息中的数据。
对getter和setter使用相同的键。