Create Notification App in Android?

时光总嘲笑我的痴心妄想 提交于 2019-12-10 11:42:51

问题


I want to create a sample project to show notification on click of button then when the user selects it open that activity in my app or open a url on selection of that. I have done something but I'm unable to complete the functionality.

First I am getting error to use this: @SuppressLint("NewApi")

If I am not using this I am getting the error on here

Notification noti = new Notification.Builder(this)

Activity Code

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

Why I have to use this:

`@SuppressLint("NewApi")` 

in my code? I am not getting the notification sound. Please suggest me what changes I have to make in my code.


回答1:


To add sound to your notification, use following short of code.

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

Full Code

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 is Application Context get using getApplicationContext().

Edit

To open any link to Browser using Notification, use following code.

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

Pass this intent to your PendingIntent




回答2:


Try this,

replace

Notification noti = new Notification.Builder(this);

with

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

You can't use this reference for context object from a click listener.




回答3:


Try this Function

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


来源:https://stackoverflow.com/questions/18526963/create-notification-app-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!