Android - notification manager, having a notification without an intent

31,783

Solution 1

You may pass the parameter

PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0)

instead of

null

on

notification.setLatestEventInfo(context, contentTitle, contentText, null);

Solution 2

The last parameter in setLatestEventInfo() is a PendingIntent and not an Intent. If you need the notification to not do anything when tapped is to pass an empty PendingIntent which is done as follows: PendingIntent.getActivity(context, 0, null, 0).

Solution 3

Interesting question and I would like to see if this works. I did a little digging and found a lot of ppl asking the same question. cbursk seems to have found a hack to get this intended functionality, which is to pass a Dialog to the notification intent instead of an Activity. I'm guessing the Dialog does nothing, or is programmed to dismiss itself right away, not sure. But I'm currently looking at this thread and going to test it out.

Share:
31,783
Garbit
Author by

Garbit

Bio I'm a UX Researcher and Software Engineer at Samsung AI Center - Cambridge. I explore Human-Centric AI through the design of wellbeing technologies that promote a deeper understanding of ourselves using computer vision, ubiquitous sensing, and personal data. I am an experienced full-stack developer with a keen interest in designing compelling user experiences and developing innovative products for both mobile and web. I lead products from design concepts to full implementation working alongside engineers, designers, and researchers to realise next generation technologies. I have a background in both Software Engineering and UX Research and thoroughly enjoy designing and developing civic technology that empowers and connects people through web and mobile platforms. Find our more on my portfolio site http://andygarbett.co.uk Dev Projects https://App-Movement.com a platform for communities to commission and automatically generate their own mobile apps around the real world issues that they face. http://Feed-Finder.co.uk, a mobile app that allows breastfeeding mothers to rate and review suitable breastfeeding locations. http://irismsg.io, a distributed SMS donation platform that enables individuals to donate SMS credit to solidarity groups in Greece. http://Sleepful.me, a social network to deliver cognitive behavioural therapy to those with insomnia and severe sleep disorders. http://Fearsquare.com, a Foursquare & UK Police data mashup that creates a personal crime exposure history from your Foursquare checkin data. http://collectionsdivetwmuseums.org.uk, a novel discovery interface for online heritage collections created in partnership with Tyne and Wear Archives and Museums and Microsoft Research Cambridge.

Updated on April 27, 2020

Comments

  • Garbit
    Garbit about 4 years

    I would like to be able to fire a notification to alert the users about a timer that has finished, however i do not wish to have an intent when you click the notification.

    I've tried passing in null for the intent

    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
    
    int icon = R.drawable.icon;
    CharSequence tickerText = "Hello";
    long when = System.currentTimeMillis();
    
    Notification notification = new Notification(icon, tickerText, when);
    
    CharSequence contentTitle = "My notification";
    CharSequence contentText = "Hello World!";
    
    notification.setLatestEventInfo(context, contentTitle, contentText, null);
    mNotificationManager.notify(HELLO_ID, notification);
    
  • Garbit
    Garbit almost 13 years
    I was calling from a broadcastreceiver and found that it was coming up with a null exception. However I tried this class I found (some guy was making a notification class) and its brilliant - check it out stackoverflow.com/questions/5626946/…
  • rfgamaral
    rfgamaral over 12 years
    Bot this and the accepeted answer work just fine but this one is more simple, no need to create an empty Intent when you can just pass null.
  • Balázs Édes
    Balázs Édes almost 12 years
    This was really helpful. I don't know, what an Intent created with an empty construcor should do, but it works perfectly on both 4.0 + and 2.2 for me :) Thanks!
  • Darcy
    Darcy over 11 years
    Passing null causes force close in android 4.1
  • Sakthimuthiah
    Sakthimuthiah about 11 years
    Thank you so much. You made my day