推送通知未显示-为什么?

时间:2019-09-15 07:46:21

标签: java android notifications

我试图在按下按钮6秒钟后创建通知,但该应用程序根本不显示任何通知。任何人都可以看看并尝试找出我在做什么错。

 @Override
            public void run() {

                int id = 1;
                Intent notificationIntent = new Intent(ConfirmOrderActivity.this,MainActivity.class);
                PendingIntent pIntent = PendingIntent.getActivity(ConfirmOrderActivity.this,id, notificationIntent,0);

                NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(ConfirmOrderActivity.this, DEFAULT_CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_restaurant_menu_black)
                .setContentTitle("You Have a notification!")
                .setContentText("See Your Notification")
                .setContentIntent(pIntent);

                Notification notification = nBuilder.build();

                NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

                manager.notify(id, notification);
            }
        }, 6000);

2 个答案:

答案 0 :(得分:0)

NotificationCompat.Builder具有一个属性(.setWhen(time))。参见下面的示例代码:

NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.ic_ansar_logo)
                .setContentTitle(pushModel.getTitle())
                .setContentText(pushModel.getBody())
                .setDefaults(Notification.DEFAULT_ALL)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setAutoCancel(true)
                .setSound(notificationSoundURI)
                .setPriority(Notification.PRIORITY_MAX)
                .setWhen(6000)
                .setContentIntent(resultIntent);

答案 1 :(得分:-1)

检查以下代码

 import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.RecyclerView;

import android.view.View;

import android.widget.Button;


import static android.app.NotificationChannel.DEFAULT_CHANNEL_ID;

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                generateNotification();
            }
        });

    }

    void generateNotification() {
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //Do something after 100ms
                int id = 1;
                Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
                PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), id, notificationIntent, 0);

                NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(getApplicationContext(), DEFAULT_CHANNEL_ID)
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setContentTitle("You Have a notification!")
                        .setContentText("See Your Notification")
                        .setContentIntent(pIntent);

                Notification notification = nBuilder.build();

                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

                manager.notify(id, notification);
            }
        }, 6000);
    }
}