首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android Firebase提示通知未显示

Android Firebase提示通知未显示
EN

Stack Overflow用户
提问于 2020-07-21 14:46:01
回答 1查看 293关注 0票数 1

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

下面来自Fire Base Messaging Service:

代码语言:javascript
复制
@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数据中提取。

代码语言:javascript
复制
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

我添加了

代码语言:javascript
复制
    <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" />

我还添加了

代码语言:javascript
复制
<uses-permission android:name="android.permission.VIBRATE" />

我找不到我的缺点。我需要你的建议。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-21 15:50:01

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63008851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档