Android 我可以接收来自firebase云消息的通知,但如何在用户界面上显示
公共类MyFirebaseMessagingService扩展了FirebaseMessagingService{ 私有静态最终字符串TAG=“MyFirebaseMsgService” 私有void sendNotification(字符串体){ 公共类MainActivity扩展了AppCompatActivity{Android 我可以接收来自firebase云消息的通知,但如何在用户界面上显示,android,firebase,firebase-cloud-messaging,Android,Firebase,Firebase Cloud Messaging,公共类MyFirebaseMessagingService扩展了FirebaseMessagingService{ 私有静态最终字符串TAG=“MyFirebaseMsgService” 私有void sendNotification(字符串体){ 公共类MainActivity扩展了AppCompatActivity{ private static final String TAG = "MainActivity"; TextView text = (TextView) findViewByI
private static final String TAG = "MainActivity";
TextView text = (TextView) findViewById(R.id.textView);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent=getIntent();
if(intent !=null){
String message=intent.getStringExtra("message");
text.setText(message);
}
Button btnShowToken=(Button) findViewById(R.id.button_show_token);
btnShowToken.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String token= FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Token:"+token);
Toast.makeText(MainActivity.this,token, Toast.LENGTH_SHORT).show();
}
});
}
}因此您可以尝试以下方法:
Intent intent=new Intent(this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("message",body);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0/*request code*/,intent,PendingIntent.FLAG_ONE_SHOT);
//set sound notification
Uri notifictaionSound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notifiBuilder=new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Firebase Cloud Messaging")
.setContentText(body)
.setAutoCancel(true)
.setSound(notifictaionSound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0/*ID of notification*/,notifiBuilder.build());
在MainActivityonCreate()
方法中执行以下操作:
Strng message=getIntent().getStringExtra("message");
if(message !=null){
//Create ui
}
所以我不知道您想要的ui,但这会从通知中得到消息,您只需要在活动中创建ui
说明:
这将在案例正文中创建一个带有额外字符串的intent,然后将intent设置为通知的pendingent。
当用户单击通知时,应用程序将打开,并检查用户是否通过单击通知启动,如果是,则获取您在意向中设置的额外内容并创建ui,如果不是什么都不做。您想要创建通知的原因?更好地解释您想要什么意思在我收到通知后,我希望通知的消息是正确的显示在UIpost
sendNotification()
code我是Android新手,希望你能尽可能详细:)谢谢你的建议,但我的应用程序在修改后无法打开。我在Oncreate()中添加了[TextView text=(TextView)findViewById(R.id.TextView);]。我的应用程序正在运行,但通知消息无法显示在UIS上。您是否已将setText设置为textview?
Intent intent=new Intent(this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("message",body);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0/*request code*/,intent,PendingIntent.FLAG_ONE_SHOT);
//set sound notification
Uri notifictaionSound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notifiBuilder=new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Firebase Cloud Messaging")
.setContentText(body)
.setAutoCancel(true)
.setSound(notifictaionSound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0/*ID of notification*/,notifiBuilder.build());
Strng message=getIntent().getStringExtra("message");
if(message !=null){
//Create ui
}