Search code examples
javaandroidnotificationscompatibilitynosuchmethoderror

Android Notification and NoSuchMethodError


I build a notification:

Notification.Builder builder = new Notification.Builder(
                            getApplicationContext())
                            .setTicker(
                                    getApplicationContext().getString(
                                            R.string.my_string))
                            .setSmallIcon(android.R.drawable.sym)
                            .setContentTitle(
                                    getApplicationContext().getString(
                                            R.string.my_string_two))
                            .setContentText(a.getB())
                            .setSound(
                                    RingtoneManager
                                            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                            .setVibrate(new long[] { 10001000 })
                            .setAutoCancel(true)
                            .setContentIntent(PendingIntent.getActivity(getApplicationContext(),0,
                                                    new Intent()
                                                            .setAction(Intent.ACTION_VIEW)
                                                            .setType(CallLog.Calls.CONTENT_TYPE)
                                                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
                                                    0));
                    NotificationManager nm = (NotificationManager) getApplicationContext()
                            .getSystemService(Context.NOTIFICATION_SERVICE);
                    nm.notify("interstitial_tag", 1, builder.build());

With android 4.0 i've found an error: NoSuchMethodError. How can i solve it? Do i use Notification.Compact? Thank you.


Solution

  • Can you try:

    public static void sendNotification(Context context, String info){
            NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(context);
    //title notifications
    notifyBuilder.setContentTitle(context.getString(R.string.app_name));
    //small icon
    notifyBuilder.setSmallIcon(R.drawable.ic_launcher);
    //set contentText
    notifyBuilder.setContentText(info);
    notifyBuilder.setVibrate(new long[]{100, 200, 100, 500});       notifyBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    //setAutoCancel
    notifyBuilder.setAutoCancel(true);
    
    getNotificationManager(context).notify(0, notifyBuilder.build());
    }
    
    public final static NotificationManager getNotificationManager(Context context) {
    return (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    }
    

    And when you used a new Intent(), please insert a destination class

    Intent wrapperIntent = new Intent(context, SenderBroadcast.class);
    wrapperIntent.putExtra("KEY_UID", uid);
    wrapperIntent.setData(Uri.parse("senderbroadcast://"+uid));
    wrapperIntent.setAction("REQUESTCODE_SENDERBROADCAST");
    PendingIntent.getActivity(context, RequestCode.REQUESTCODE_SENDERBROADCAST, wrapperIntent, PendingIntent.FLAG_UPDATE_CURRENT);