0

我遵循此tutorial,當我嘗試從Firebase控制檯發送消息時,調用了onMessageReceived並執行了createNotification,但沒有顯示通知提示。收到Android Firebase通知消息但未顯示通知提示?

它想提示這一點,但它並沒有

it suppose to show prompt like this

下面是我MyAndroidFirebaseMsgService代碼

import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.BitmapFactory; 
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 java.util.HashMap; 
public class MyAndroidFirebaseMsgService extends FirebaseMessagingService 
{ 
    private static final String TAG = "MyAndroidFCMService"; 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
    //Log data to Log Cat 
    Log.d(TAG, "From: " + remoteMessage.getFrom()); 
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 
    //create notification 
    createNotification(remoteMessage.getNotification().getBody()); 
    } 

    private void createNotification(String messageBody) 
    { 
    Intent intent = new Intent(this , ResultActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent resultIntent = PendingIntent.getActivity(this , 0, intent, PendingIntent.FLAG_ONE_SHOT); 

    Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(this) 
     .setSmallIcon(R.mipmap.ic_launcher) 
     .setContentTitle("Test Notification") 
     .setContentText(messageBody) 
     .setAutoCancel(false) 
     .setSound(notificationSoundURI) 
     .setContentIntent(resultIntent); 

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

    notificationManager.notify(0, mNotificationBuilder.build()); 
    } 
} 
+0

發佈您的'MyAndroidFirebaseMsgService'代碼 –

+0

當您的應用程序位於後臺時會發生這種情況。假設是,當它的可見,應用程序已經在焦點,你會執行任何你想在那裏然後。 –

+0

它更好地使用您的自定義通知onMessageReceived或首先檢查日誌是否使用日誌或調試接收通知 –

回答

1

最後發現,該通知實際上只出現在Android下拉菜單,而不是彈出,由於我的設備運行Android 4.我再次用Android 5設備進行測試並彈出通知就像教程一樣。

0
Maybe you were missing notification notify. 
Please try bellow code. it's working properly 

//This method is only generating push notification 
    //It is same as we did in earlier posts 
    private void sendNotification(RemoteMessage remoteMessage) { 
     Intent resultIntent = new Intent(this, MenuBarActivity.class); 
     resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     HashMap<String, String> dataMap = new HashMap<String, String>(remoteMessage.getData()); 
     resultIntent.putExtra(Constant.KEY_MESSAGE_BODY, dataMap); 
     resultIntent.putExtra(Constant.KEY_IS_NOTIFICATION, true); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle(getString(R.string.app_name)) 
       .setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getData().get(Constant.KEY_BODY))) 
       .setContentText(remoteMessage.getData().get(Constant.KEY_BODY)) 
       .setAutoCancel(true) 
       .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) 
       .setSound(defaultSoundUri); 

//  if (!BaseActivity.isInForeground()) { 
     notificationBuilder.setContentIntent(pendingIntent); 
//  } 
     // Generate ID 
     long time = new Date().getTime(); 
     String tmpStr = String.valueOf(time); 
     String last4Str = tmpStr.substring(tmpStr.length() - 5); 
     int notificationId = Integer.valueOf(last4Str); 

     // Sets an ID for the notification 
//   int notificationId = Constant.NOTIFICATION_ID; 

     // Gets an instance of the NotificationManager service 
     NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

     // Builds the notification and issues it. 
     mNotifyMgr.notify(notificationId, notificationBuilder.build()); 
    } 
+0

我得到了無法解析符號的錯誤常量和日期() – Eddi

+0

我設法修復常量和date()錯誤,但即使使用函數,通知提示符也不會顯示出來。您的功能中的所有代碼都已完全執行,我也可以聽到通知聲音。 – Eddi