简体   繁体   中英

native notification android code in cordova plugin java class

I'm some what aware of cordova and now I'm implementing a notification in my plugin class.

I have used the below android code to create the notification in my java class. I have imported all the packages related to notification. But i'm getting the notification exception and unable to get the notification

NotificationManager notificationManager = (NotificationManager)this.getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
    Notification notification=new Notification.Builder(getApplicationContext())
    .setWhen(System.currentTimeMillis())
    .setContentTitle("Welcome")
    .setTicker("Click here to see the offers...")
    .setContentText("Click here to see the offers...")
    //.setSmallIcon(1)
    .setAutoCancel(true)
    .build();

    notificationManager.notify(info, notification);

And the exception I'm getting is

NotificationService(802): Not posting notification with icon==0:     Notification(pri=0 icon=0 contentView=com.ionicframework.dummy292686/0x1090085 vibrate=null sound=null defaults=0x0 flags=0x10 when=1439564895752 ledARGB=0x0 contentIntent=N deleteIntent=N contentTitle=7 contentText=31 originalPackageName=N originalUserId=0 tickerText=31 kind=[null])

Can some body give suggestions to write the notification logic in a cordova plugin java class?

I believe you need to provide at least a small icon.

Uncomment .setSmallIcon(1) and replace the 1 with resource ID for a drawable to be used as the icon in the notification.

Finally i have done some changes in building the notification object and it worked for me.

try{
        Log.i(TAG, "In try block of notification");
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
        Notification notification = new Notification.Builder(this)
        .setTicker("My Company")
        .setContentTitle("Welcome")
        .setContentText("Click here to see offers...")
        .setSmallIcon(R.drawable.icon)
        .setContentIntent(pIntent).getNotification();
        notification.flags=Notification.FLAG_AUTO_CANCEL;
        NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, notification);
    }catch(Exception e){
        Log.e(TAG, "In catch block of notification:"+e);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM