2016-12-24 122 views
0
package com.bamart.mybhaskarmart.activity; 

import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.media.RingtoneManager; 
import android.net.Uri; 
import android.support.v4.app.NotificationCompat; 
import android.util.Log; 

import com.google.firebase.messaging.FirebaseMessagingService; 
import com.google.firebase.messaging.RemoteMessage; 

import mypackage.Bhaskar.mybhaskarmart.R; 

/** 
* Created by Anil on 12/24/2016. 
*/ 
public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMsgService"; 

    public MyFirebaseMessagingService() { 
    super(); 
    } 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     //Displaying data in log 
     //It is optional 
     Log.d(TAG, "From: " + remoteMessage.getFrom()); 
     Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 

     //Calling method to generate notification 
     sendNotification(remoteMessage.getNotification().getBody()); 
    } 

    //This method is only generating push notification 
    //It is same as we did in earlier posts 
    private void sendNotification(String messageBody) { 
     Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.swadeshmarticon) 
       .setContentTitle("Firebase Push Notification") 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(0, notificationBuilder.build()); 
    } 


    @Override 
    protected Intent zzae(Intent intent) { 
     return null; 
    } 
} 

爲什麼zzae ovveriden方法是有我收到回電時,我火從火力caonsole 消息這是我的火力地堡的代碼,當我運行這段代碼我無法得到消息,我沒有得到通知知道爲什麼消息沒有收到,而我正在做的每一件事情都是正確的,請告訴我。無法使用火力地堡

+0

可以添加您的清單文件 –

+0

OK等待我送的出手http://paste.ofcode.org/ 3xbH7iJCbBFRgqhVNzCbGr –

回答

0

編輯PendingIntent.FLAG_ONE_SHOT與PendingIntent.FLAG_UPDATE_CURRENT

試試這個: -

public class MyFirebaseMessagingService extends FirebaseMessagingService { 
    private static final String TAG = "FCM Service"; 
    NotificationManager notificationManager; 
    Notification notification; 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     super.onMessageReceived(remoteMessage); 
     // TODO: Handle FCM messages here. 
     // If the application is in the foreground handle both data and notification messages here. 
     // Also if you intend on generating your own notifications as a result of a received FCM 
     // message, here is where that should be initiated. 
     Log.d(TAG, "From: " + remoteMessage.getFrom()); 
     Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 


     sendNotification(remoteMessage.getNotification().getBody(),remoteMessage.getNotification().getTitle(),remoteMessage.getSentTime()); 
    } 

    private void sendNotification(String msg,String title,long time) { 
     Intent notifyintent=new Intent(getApplicationContext(),MainActivity.class); 
     //int id= intent.getExtras().getInt("notificationId"); 
     int id= 0; 

     PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),0,notifyintent,PendingIntent.FLAG_UPDATE_CURRENT); 
     notificationManager =(NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 
     notification= new NotificationCompat.Builder(getApplicationContext()) 
       .setContentTitle(title) 
       .setContentText(msg) 
       .setWhen(time) 
       .setDefaults(Notification.DEFAULT_SOUND) 
       .setAutoCancel(true) 
       .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark) 

       .setContentIntent(pendingIntent).build(); 
     notificationManager.notify(id,notification); 
    } 
} 
+0

爲什麼在zzae上不會回覆onMessage –

+0

,也沒有在後臺狀態獲取通知@Nainal –

+0

我們只能在應用處於Firebase基本控制檯的前臺時發送通知。要在後臺發送通知,您需要進行通話。參考鏈接https://firebase.google.com/docs/cloud-messaging/android/receive http://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background- in-firebase – Nainal