使用NotifyCompat.Builder不显示通知

时间:2017-01-03 11:05:49

标签: java android notifications

我已将oncreate mainactivity方法中的方法调用到调用notification的方法但是在应用启动时未通知 MainActivity oncreate方法

      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

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;
    }
}

2 个答案:

答案 0 :(得分:3)

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

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));

答案 1 :(得分:1)

你忘了放一个小图标。

.setSmallIcon(R.drawable.my_icon)