在firebase通知中接收图标和声音时出错

时间:2017-08-14 12:16:16

标签: android firebase firebase-cloud-messaging android-notifications android-notification-bar

所以,我正在开发一款Android应用,它会收到来自Firebase的通知。要接收它们,没有问题,但问题是我只能在应用程序打开时自定义它们在用户设备中显示的方式,并且它位于主屏幕中。因此,当应用程序关闭时,通知没有图标,没有声音,也没有振动。

您必须明白,但是我已经在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);
 }

}

1 个答案:

答案 0 :(得分:0)

如果要自定义通知,则不应使用firebase控制台。
Look here for more information

您还应该为图片添加以下代码:

JavaExec

第二件事是关于通知声音,如果你想要自定义声音,请使用:

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