【发布时间】:2016-11-28 04:40:12
【问题描述】:
我想通过广播接收器在活动的快餐栏中显示 firebase 推送通知(发送 firebase 控制台),这是我从 FirebaseMessagingService 获得的,但我无法显示。请帮助我.
清单:
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
firebase 消息服务:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if(remoteMessage.getData().size()>0){
Log.v("Message data", remoteMessage.getData().toString());
}
if(remoteMessage.getNotification()!=null){
Log.v("MessageNotification", remoteMessage.getNotification().getBody().toString());
}
if(remoteMessage!=null){
sendNotification(remoteMessage.toString());
}
}
private void sendNotification(String messageBody){
Intent intent=new Intent(this,NotificationShowActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("FireBaseNotification",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)
.setSmallIcon(R.mipmap.notification_icon)
.setContentTitle("Notification")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
notificationShow 活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_show);
linearLayout=(RelativeLayout)findViewById(R.id.activity_notification_show);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(broadcastReceiver,new IntentFilter(MyFirebaseMessagingService.NOTIFICATION_SERVICE));
}
//broadcastReceiver
private BroadcastReceiver broadcastReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String message=intent.getStringExtra("FireBaseNotification");
if(message!=null){
Toast.makeText(NotificationShowActivity.this,message,Toast.LENGTH_LONG).show();
}
}
};
}
【问题讨论】:
-
你如何发送数据,是使用 Firebase 控制台还是你已经实现了自己的服务器?它是否显示在您的日志语句中?
-
您的 Android 监视器中是否显示 Log 语句?
-
是........,,,,
-
实现自己的服务器
标签: android firebase-cloud-messaging