简体   繁体   中英

status bar notification not showing

I have a simple method which onclick() of a Button should generate status bar notification .But I don't know why it's not showing .

public void showNotification()
{


     NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(this.NOTIFICATION_SERVICE);


        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(MainActivity.this);

        mBuilder.setContentTitle("Status Bar Multiline");
        mBuilder.setContentText("Hello ! I am there and i am captains of the sky ,I got something work");
         mNotificationManager.notify(10,mBuilder.build());

}

this method I am calling onclick() of a Button

try this

 Notification notification = new Notification(icon, tickerText, when);
 notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
 .
 .
 .
 notificationManager.notify(100, notification);




 long when = System.currentTimeMillis();

you can try this

NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setContentTitle("Test notification").setContentText("this is test application.").setSmallIcon(R.drawable.ic_launcher);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(new Intent(this, MainActivity.class));



PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);



NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());

For android to put your notification on the status bar, you should set a small icon for it. ie,

mBuilder.setSmallDrawable(R.mipmap.ic_launcher); //Use your icon here. I am using the app's default icon
mBuilder.setContentTitle("Status Bar Multiline");
mBuilder.setContentText("Hello ! I am there and i am captains of the sky ,I got something work");
mNotificationManager.notify(10,mBuilder.build());

try this:

Notification.Builder nb = new Notification.Builder(GCMIntentService.this);

        nb.setSmallIcon(mSmallIconRes)
            .setContentTitle(mContentTitle)
            .setContentText(mContentText)
            .setStyle(new Notification.BigTextStyle().bigText(mContentText))
            .setTicker(mTicker)
            .setLargeIcon(result == null ? mDefaultLargeIcon : result)
            .setSubText("")
            .setContentInfo(mContentInfo)               
            .setWhen(mWhen)
            .setSound(alarmSound);

        Intent notificationIntent = new Intent(GCMIntentService.this, dogs_online.class);
        nb.setAutoCancel(true);

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(GCMIntentService.this, 0, notificationIntent, 0); 
        nb.setContentIntent(pendingIntent);

        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);          
        super.onPostExecute(result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM