使用Firebase控制台发送推送通知

时间:2018-12-12 22:41:37

标签: android notifications firebase-cloud-messaging push

我想从firebase向我的应用程序的用户发送通知,但是当我在firebase控制台中编写消息时,没有用户收到通知。我该怎么办?

这是我的MainActivity中的东西

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(Constants.CHANNEL_ID, Constants.CHANNEL_NAME, importance);
        mChannel.setDescription(Constants.CHANNEL_DESCRIPTION);
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.RED);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        mNotificationManager.createNotificationChannel(mChannel);
    }

那是我的NotificationManager

public class MyNotificationManager {
private Context mCtx;
private static MyNotificationManager mInstance;

private MyNotificationManager(Context context) {
    mCtx = context;
}

public static synchronized MyNotificationManager getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new MyNotificationManager(context);
    }
    return mInstance;
}

public void displayNotification(String title, String body) {

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(mCtx, Constants.CHANNEL_ID)
                    .setSmallIcon(R.mipmap.ic_yafe)
                    .setContentTitle(title)
                    .setContentText(body);
    Intent resultIntent = new Intent(mCtx, MainActivity.class);

    PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(pendingIntent);
    NotificationManager mNotifyMgr =
            (NotificationManager) mCtx.getSystemService(NOTIFICATION_SERVICE);
    if (mNotifyMgr != null) {
        mNotifyMgr.notify(1, mBuilder.build());
    }
}
}

0 个答案:

没有答案