创建通知时出现问题

时间:2014-06-06 12:29:27

标签: android android-notifications

我写了两个通知函数。它们都在工作 - 它只是旧版本使用弃用的方法。问题是我的新菜单没有显示在菜单栏上,并且在出现时不会振动或发出噪音。我需要添加什么?

旧版本:

public void createNotification(String message) {    
    Intent intent = new Intent(this, NotificationService.class);    
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);     
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);     
    Notification n = new Notification(R.drawable.logo_small, message,    
            System.currentTimeMillis());    

    n.setLatestEventInfo(this, message, message, pi);    
    n.defaults = Notification.DEFAULT_ALL;     
    nm.notify(12327942 + notidchange, n);     
    ++notidchange;
}     

新版本:

public void createNotification(String message) {
    //add diff icons and titles later
    Intent intent = new Intent(this, NotificationService.class);
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setContentTitle(message)
            .setContentText(message)
            .setSmallIcon(R.drawable.logo_small)
            .setAutoCancel(true) //do I want?
            .setContentIntent(pi);
    nm.notify(12327942 + notidchange++, builder.build());
}

1 个答案:

答案 0 :(得分:0)

在nm.notify之前

,把这段代码

Notification notif = builder.build();
notif.flags |= Notification.FLAG_SHOW_LIGHTS;
notif.ledOnMS = 100; 
notif.ledOffMS = 100;
notif.defaults = Notification.DEFAULT_ALL;

并替换nm.notify(12327942 + notidchange++, builder.build());nm.notify(12327942 + notidchange++, notif);

相关问题