java推送数据给安卓,java – 如何从Firebase推送通知中获取数据并将其显示在Android Activity中?...
對(duì)不起這個(gè)noob問題,我是
android開發(fā)的新手.我目前正在開發(fā)一個(gè)項(xiàng)目,需要向安裝了我的應(yīng)用程序的
Android設(shè)備發(fā)送推送通知.我已經(jīng)按照firebase的快速入門教程完成了這項(xiàng)工作,并在我的設(shè)備上成功收到了通知.
問題:如何檢索服務(wù)器發(fā)送的消息并將該消息顯示給我的Android Activity?提前致謝.
繼承我的代碼. MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.tv);
/*
* Get the data from push notification and display it in TextView
* */
tv.setText("Message retrieve from Push Notification");
}
這是我的MyFirebaseMessagingService.java
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
sendNotification(remoteMessage.getNotification().getBody());
}
//This method is only generating push notification
//It is same as we did in earlier posts
public void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.putExtra("message", messageBody);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Peoplelink Push Notification")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的java推送数据给安卓,java – 如何从Firebase推送通知中获取数据并将其显示在Android Activity中?...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么把音频转换文字?三个步骤解决它
- 下一篇: oracle批量update数据_东方国