1

我想创建一个示例项目以在单击按钮时显示通知,然后当用户选择它时在我的应用程序中打开该活动或打开一个选择该活动的 url。我做了一些事情,但我无法完成功能。

首先我收到错误使用这个:@SuppressLint("NewApi")

如果我不使用它,我会在这里收到错误

Notification noti = new Notification.Builder(this)

活动代码

public class NotificationExample extends Activity implements OnClickListener{

    private Button b;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification_example);
        b=(Button)findViewById(R.id.button1);
        b.setOnClickListener(this);

    }


    @SuppressLint("NewApi")
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
        Notification noti = new Notification.Builder(this)
        .setTicker("Ticker Title")
        .setContentTitle("Content Title")
        .setContentText("Notification content.")
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentIntent(pIntent).getNotification();
        noti.flags=Notification.FLAG_AUTO_CANCEL;
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, noti); 
    }
}

为什么我必须使用这个:

`@SuppressLint("NewApi")` 

在我的代码中?我没有收到通知声音。请建议我必须对代码进行哪些更改。

4

3 回答 3

2

要为您的通知添加声音,请使用以下短代码。

Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(notificationSound);

完整代码

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
                0);
Builder builder = new NotificationCompat.Builder(context)
                .setTicker("Ticker Title").setContentTitle("Content Title")
                .setContentText("Notification content.")
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pIntent);
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(notificationSound);
Notification noti = builder.build();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);

context正在Application Context使用getApplicationContext()

编辑

要使用通知打开任何指向浏览器的链接,请使用以下代码。

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.google.com"));

将此意图传递给您的PendingIntent

于 2013-08-30T07:25:16.353 回答
1

尝试这个,

代替

Notification noti = new Notification.Builder(this);

Notification noti = new Notification.Builder(NotificationExample .this);

您不能将此引用用于来自单击侦听器的上下文对象。

于 2013-08-30T07:10:50.650 回答
0

试试这个功能

public static void addToNotificationBar(Context mContext,String ticker, String title, String message) {
        long when = System.currentTimeMillis();
        int icon = R.drawable.ic_launcher;

        NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, new Intent(), 0);
        Notification notification = new Notification(icon, ticker, when);
        notification.setLatestEventInfo(mContext, title, message, contentIntent);
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
    }
于 2013-08-30T07:19:25.613 回答