Android通知无法启动

时间:2016-11-07 12:22:50

标签: android android-notifications

我是一个Asynctask类,在onProgressUpdate中我想在状态栏中创建通知,我已经这样做了:

    @Override
protected void onProgressUpdate(Object[] values) {

    this.holder.itemView.setBackgroundColor(Color.WHITE);
    if(messaggio){

        PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, EUDroid_Main_Activity.class), 0);
        Notification notification = new NotificationCompat.Builder(context)
                .setTicker("Ticker")
                .setContentTitle("Title")
                .setContentText("Hello")
                .setContentIntent(pi)
                .setAutoCancel(true)
                .build();

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, notification);


    }
    super.onProgressUpdate(values);

}

但通知不明星为什么? 我该如何创建通知?

编辑我已经这样做了:

@Override
protected void onProgressUpdate(Object[] values) {

    this.holder.itemView.setBackgroundColor(Color.WHITE);
    if(messaggio){

        try {
            Thread.sleep(Integer.parseInt(ritardoMessaggio)*1000);
        } catch (InterruptedException e){
            e.printStackTrace();
        }

        NotificationCompat.Builder notification;
        PendingIntent pIntent;
        NotificationManager manager;
        Intent resultIntent;
        TaskStackBuilder stackBuilder;

        notification = new NotificationCompat.Builder(context);

        notification.setContentTitle(nameFile);

        notification.setContentText(testoMessaggio);

        notification.setTicker("Evento EUDroid");

        notification.setSmallIcon(R.drawable.splash);

        notification.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
        notification.setLights(Color.BLUE, 500, 500);
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        notification.setSound(alarmSound);

        //Creating new Stack Builder
        stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(EUDroid_Main_Activity.class);
        //Intent which is opened when notification is clicked
        resultIntent = new Intent(context, EUDroid_Main_Activity.class);
        stackBuilder.addNextIntent(resultIntent);
        pIntent =  stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setContentIntent(pIntent);
        manager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0, notification.build());


    }
    super.onProgressUpdate(values);

}

现在我有这个问题: 当我发出通知时会发出两声嘟嘟声为什么?我如何收到两个通知

1 个答案:

答案 0 :(得分:0)

  

试试!!这段代码肯定会奏效。

 NotificationManager manager;
 Notification myNotication;
    manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    btnShow.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {


            //API level 11
            Intent intent = new Intent("com.example.sampad.HOMEFRAGMENT");

            PendingIntent pendingIntent = PendingIntent.getActivity(PushNotification.this, 1, intent, 0);

            final Notification.Builder builder = new Notification.Builder(PushNotification.this);



            builder.setAutoCancel(false);
            builder.setTicker("You have got a notificatio");
            builder.setContentTitle("Request Approval Notification");
            builder.setContentText("send you a request for approval");
            builder.setSmallIcon(R.drawable.appicon);
            builder.setContentIntent(pendingIntent);
            builder.setOngoing(true);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                builder.setSubText("This is subtext...");   //API level 16
            }
            builder.setNumber(100);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                builder.build();
            }
             /* Add Big View Specific Configuration */


            myNotication = builder.getNotification();
            manager.notify(11, myNotication);


            /*
            //API level 8
            Notification myNotification8 = new Notification(R.drawable.ic_launcher, "this is ticker text 8", System.currentTimeMillis());

            Intent intent2 = new Intent(MainActivity.this, SecActivity.class);
            PendingIntent pendingIntent2 = PendingIntent.getActivity(getApplicationContext(), 2, intent2, 0);
            myNotification8.setLatestEventInfo(getApplicationContext(), "API level 8", "this is api 8 msg", pendingIntent2);
            manager.notify(11, myNotification8);
            */

        }
    });