0

所以,我開發一個Android應用程序,它接收來自火力地堡的通知。要接收它們,沒有問題,但問題是我只能在應用程序打開並且在主屏幕中時自定義它們顯示在用戶設備中的方式。因此,當應用程序關閉時,通知不帶圖標,沒有聲音,也沒有振動。錯誤而在火力通知接收圖標和聲音

你必須明白,但是我已經改變了我在NotificationCompat類想,這些配置只是沒有在應用程序而關閉時適用。請參閱下面的代碼。

所以,我希望,我已經可以理解的,如果有人有可以說發生了什麼,我會非常感激。

類接收通知

public class MyFirebaseMessagingService extends FirebaseMessagingService { 


private static final String TAG = "MyFirebaseMsgService"; 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 


    Log.d(TAG, "From: " + remoteMessage.getFrom()); 

    if (remoteMessage.getData().size() > 0) { 
    Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
    } 

    if (remoteMessage.getNotification() != null) { 
     Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
    } 



    notifyuer(remoteMessage.getFrom(), remoteMessage.getNotification().getBody()); 
    // Also if you intend on generating your own notifications as a result of a received FCM 
    // message, here is where that should be initiated. See sendNotification method below. 
} 

public void notifyuer(String from, String notification){ 
    MyNotificationManager myNotificationManager = new MyNotificationManager(getApplicationContext()); 
    myNotificationManager.showNotificacao(from,notification, new Intent(getApplicationContext(),MainActivity.class)); 

} 

自定義通知類

public class MyNotificationManager { 
private Context context; 

public MyNotificationManager(Context context){ 
    this.context = context; 
} 

public void showNotificacao(String from, String notification, Intent intent){ 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 /* Request code */, intent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 



    //long[] vibrar = {150,400,150,800}; 


    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context); 

      Notification mNotification = notificationBuilder 
      .setContentIntent(pendingIntent) 
      .setSmallIcon(R.drawable.logocinza64) 
      .setContentTitle("S.I.C.C.") 
      .setContentText(notification) 
      .setAutoCancel(true) 
      .setVibrate(new long[]{ 100, 250, 100, 500, 800}) 
      .build(); 

    mNotification.flags |= Notification.FLAG_AUTO_CANCEL; 


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

    try{ 
     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     Ringtone toque = RingtoneManager.getRingtone(context,defaultSoundUri); 
     toque.play(); 

    }catch (Exception e){ 

    } 

    notificationManager.notify(0 /* ID of notification */, mNotification); 
} 

} 

回答

0

如果你想定製的通知,你不應該使用火力控制檯。
Look here for more information

您還應該添加如下代碼的圖像:

Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher); 
notificationBuilder.setLargeIcon(largeIcon) 

第二件事是關於通知的聲音,如果你想自定義音效,使用此:

Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd); 
notificationBuilder.setSound(uri); 
+0

謝謝回答人,但是在閱讀完您通過的內容後,我仍然不明白如何在通知中傳遞圖標或聲音,並在應用程序處於通知狀態時對待它們,因爲我正在使用Firebase雲端函數發送通知在應用程序中,我沒有收到之前通過的圖標。 –

+0

我更新了答案 – mac229