【问题标题】:Create Notification App in Android?在 Android 中创建通知应用程序?
【发布时间】:2013-08-30 07:08:35
【问题描述】:

我想创建一个示例项目以在单击按钮时显示通知,然后当用户选择它时在我的应用程序中打开该活动或打开一个选择该活动的 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")` 

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

【问题讨论】:

  • 为什么我必须在我的代码中使用这个@SuppressLint("NewApi")。它没有给我通知声音
  • @SuppressLint("NewApi") 是 Android Lint 工具使用的注解。
  • 以及为什么我没有收到通知声音

标签: android android-intent android-notifications


【解决方案1】:

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

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);

contextApplication Context 使用getApplicationContext()

编辑

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

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

将此意图传递给您的PendingIntent

【讨论】:

  • @chitan 当用户打开通知时我怎么能在其中打开一个 URL
  • 是的,实际上当用户打开它时我可能必须打开一个新活动或一个 URL ..所以我得到了新活动的解决方案帮助我如何打开一个 URL
  • 你有什么要给我的
  • 如果你能帮助我,你会看到这个吗stackoverflow.com/questions/18529802/…
【解决方案2】:

试试这个,

替换

Notification noti = new Notification.Builder(this);

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

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

【讨论】:

  • 为什么我必须在我的代码中使用这个@SuppressLint("NewApi")。它没有给我通知声音
【解决方案3】:

试试这个功能

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);
    }

【讨论】:

  • 请解释我必须在这个函数中传递的参数,点击按钮我会调用这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-23
  • 1970-01-01
相关资源
最近更新 更多