Android Firebase抬头通知未显示

时间:2020-07-21 06:46:00

标签: java android firebase push-notification

我需要发送提醒通知,但无法发送。通知显示在通知托盘中。但是抬头通知未显示

以下来自Fire Base Messaging Service:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData().toString());
        String title1 = remoteMessage.getData().get("title");
        String message1 = remoteMessage.getData().get("body");
        sendNotification(getApplicationContext(), title1, message1);

    }
}


private void sendNotification(Context context, String title1, String message1) {
    Intent intent = new Intent(context, HomeActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);
    String channelId = getString(R.string.default_notification_channel_id);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    int notificationID = (int) System.currentTimeMillis(); 
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(context, CHANNEL_ID)
                    .setSmallIcon(R.drawable.collegepic)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.collegepic))
                    .setContentTitle(title1)
                    .setContentText(message1)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setFullScreenIntent(pendingIntent, true)
                    .setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE) //Important for heads-up notification
                    .setPriority(Notification.PRIORITY_MAX); //Important for heads-up notification;

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,"Channel human readable title",NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(channel);
    }

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

从FCM json数据中提取。

private void sendUpstreamNotification() {
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    JSONObject jsonObject = new JSONObject();
    JSONObject objData = new JSONObject();


    final String url = "image_url";

    try{
        objData.put("title", "Message From BATTI");
        objData.put("body","Important notice for You. Plz check.");
        objData.put("image", R.drawable.collegenot);
        objData.put("icon", R.drawable.collegenot);
        objData.put("sound", "default");
        objData.put("android_channel_id ", "1");
        objData.put("content_available","true");
        objData.put("priority", "high");


        jsonObject.put("to", "/topics/" + SUBSCRIBE_TO);
        jsonObject.put("data", objData);

        String URL = FCM_API;
        JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.POST, FCM_API,
        jsonObject,new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.i(TAG, "onResponse: " + response.toString());
                Toast.makeText(UploadImageActivity.this, "Request successful", Toast.LENGTH_LONG).show();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(UploadImageActivity.this, "Request error", Toast.LENGTH_LONG).show();
                Log.i(TAG, "onErrorResponse: Didn't work");
            }
        }) {
            @Override
            public Map<String, String> getHeaders() {
                Map<String, String> header = new HashMap<>();
                header.put("Content-Type", contentType);
                header.put("Authorization", serverKey);
                return header;
            }
        };


        requestQueue.add(objectRequest);

    }catch (JSONException e){
        e.printStackTrace();
    }
}

来自Manifest.xml

我添加了

    <service
        android:name=".MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/collegenot" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />

我还添加了

<uses-permission android:name="android.permission.VIBRATE" />

我无法找出我的错。我需要你的建议。

1 个答案:

答案 0 :(得分:0)

您从Channel制作了string resource,但是您在制作Notification且常量为CHANNEL_ID的对象。

相关问题