2016-11-27 45 views
0

我想告訴火力推送通知通過廣播接收機活動的小吃吧(發送火力控制檯)這是我從FirebaseMessagingService得到,但我無法show.please幫我。火力通知,小吃吧不顯示,而應用程序的Android封閉

清單:

<service android:name=".MyFirebaseMessagingService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
      </intent-filter> 
     </service> 

火力消息服務:

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(); 

      } 
     } 
    }; 

} 
+0

如何ü發送數據,使用火力地堡控制檯,或者你已經實現了你擁有服務器?它是否顯示在您的Log語句中? – Manny265

+0

firebase控制檯 – AAA

+0

是在Android監視器中顯示的日誌語句嗎? – Manny265

回答

0

趕上背景你應該使用接收器,而不是服務FCM消息。

public class NotificationsReceiver extends WakefulBroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     //handle your data 
     abortBroadcast(); //stop sending this broadcast to other receivers. 
    } 
} 

在你的清單(重要 - 設置優先級爲999,是第一個在接收隊列):

<receiver 
     android:name=".fcm.NotificationsReceiver" 
     android:exported="true" 
     android:permission="com.google.android.c2dm.permission.SEND"> 
     <intent-filter android:priority="999"> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
     </intent-filter> 
    </receiver> 
相關問題