gpt4 book ai didi

java - 使用 : NotificationCompat. Builder 不显示通知

转载 作者:行者123 更新时间:2023-11-29 08:39:25 25 4
gpt4 key购买 nike

我已将 mainactivityoncreate 方法中的一个方法调用到一个调用 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());}

决策.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,您的通知将生成但不会显示在状态中酒吧

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

关于java - 使用 : NotificationCompat. Builder 不显示通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41442075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com