【问题标题】:Android Firebase Heads-up notification not showingAndroid Firebase 提醒通知未显示
【发布时间】:2020-07-21 06:46:00
【问题描述】:

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

以下来自 Fire Base 消息服务:

@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" />

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

【问题讨论】:

  • 您正在发送通知消息。您应该发送数据消息。检查this
  • 你可以试试我的源代码吗? stackoverflow.com/a/50464316/4729203
  • @M D 我正在发送数据消息。通知显示在通知托盘中。但未显示为抬头通知
  • @wonsuc 你会检查我的代码吗?我在代码中找不到我的错误,为什么不显示抬头通知
  • 我不确定。 CHANNEL_ID 来自哪里?

标签: java android firebase push-notification


【解决方案1】:

您从string resource 中创建了Channel,但您正在使用CHANNEL_ID 常量创建Notification 对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-01
    • 2013-04-09
    • 2019-08-05
    相关资源
    最近更新 更多