通知不显示使用:NotificationCompat.Builder [英] Notification is not displayed using : NotificationCompat.Builder

查看:95
本文介绍了通知不显示使用:NotificationCompat.Builder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 mainactivityoncreate 方法中调用了一个方法到一个调用 notification 的方法,但是在应用程序启动时没有传递通知MainActivity oncreate 方法

I have called a method in oncreate method of mainactivity to a method calling notification but the notification is not delivered when the app starts MainActivity oncreate method

      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView) findViewById(R.id.view);

        getOverflowMenu();
        Decision.notify_user(getApplicationContext());}

Decision.java

Decision.java

public class Decision {
    public final static String YES="Done";
    public final static String NO="SORRY";
    public final static int NOTIFICATIN_ID=1568;

    public static void notify_user(Context context)
    {
        NotificationCompat.Builder builder=new NotificationCompat.Builder(context)
                .setColor(ContextCompat.getColor(context, R.color.colorPrimaryDark))
                .setContentTitle("Have you done it")
                .setContentText("Make sure you do it")
                .setStyle(new NotificationCompat.BigTextStyle().bigText("done"))
                .setContentIntent(contentIntent(context))
                .addAction(decided_yes(context)).addAction(decided_no(context));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            builder.setPriority(Notification.PRIORITY_HIGH);
        }
        NotificationManager notificationManager=(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATIN_ID,builder.build());

    }

    public static PendingIntent contentIntent(Context context)
    {
        Intent intent=new Intent(context,MainActivity.class);
        return PendingIntent.getActivity(context,123,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    }

    public static NotificationCompat.Action decided_yes(Context context)
    {
        Intent intent=new Intent(context,MainActivity.class);
        intent.setAction(YES);
        PendingIntent pendingIntent=PendingIntent.getActivity(context,19848,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Action action=new NotificationCompat.Action(R.mipmap.ic_launcher,"Yes",pendingIntent);
        return action;
    }

    public static NotificationCompat.Action decided_no(Context context)
    {
        Intent intent=new Intent(context,MainActivity.class);
        intent.setAction(NO);
        PendingIntent pendingIntent=PendingIntent.getActivity(context,1948,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Action action=new NotificationCompat.Action(R.mipmap.ic_launcher,"no",pendingIntent);
        return action;
    }
}

推荐答案

您需要使用 Notification.Builder 设置一些 icon 因为没有 icon您的通知将被生成,但不会显示在状态栏

You need to set some icon with your Notification.Builder because without icon your notification will be generated but will not be displayed on status bar

NotificationCompat.Builder builder=new NotificationCompat.Builder(context)
            .setColor(ContextCompat.getColor(context, R.color.colorPrimaryDark))
            .setContentTitle("Have you done it")
            .setContentText("Make sure you do it")
            .setSmallIcon(R.drawable.yourIconID)
            .setStyle(new NotificationCompat.BigTextStyle().bigText("done"))
            .setContentIntent(contentIntent(context))
            .addAction(decided_yes(context)).addAction(decided_no(context));

这篇关于通知不显示使用:NotificationCompat.Builder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆