在Android中创建通知应用程序?

时间:2013-08-30 07:08:36

标签: android android-intent android-notifications

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

首先我收到错误使用它:@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")` 

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

试试这个,

替换

Notification noti = new Notification.Builder(this);

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

您不能对点击侦听器使用引用作为上下文对象。

答案 2 :(得分: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);
    }