I am trying to create a notification for android device. This is the code written on the mobile. It shows the notification on the mobile phone but does not show up in android wear. Am I missing out on something?
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Log.i("Tag","Inside Self destruct");
//Code for notification begins here
PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, ShowNotificationDetailActivity.class), 0);
Resources r = getResources();
String greetingIdText = r.getString(R.string.notification_text);
Notification notification = new NotificationCompat.Builder(context)
.setTicker(r.getString(R.string.notification_title))
.setSmallIcon(android.R.drawable.ic_menu_report_image)
.setContentTitle(r.getString(R.string.notification_title))
.setContentText(greetingOne.getContent())
.setContentIntent(pi)
.setAutoCancel(true)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(0, notification);
}
});
Related
I am trying to send a notification when the button is pressed, and when the user clicks the notification the activity ActPending should starts.
the below posted code did not either send a notification nor starts the ActPending.
please let me know what I did wrong and how to fix it.
code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_main);
mBtn_send = (Button) findViewById(R.id.btn_send_notification);
mBtn_send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
});
}
public void send() throws PendingIntent.CanceledException {
Intent intAct = new Intent(this, ActPending.class);
PendingIntent pending = PendingIntent.getActivity(this, 1, intAct, PendingIntent.FLAG_UPDATE_CURRENT);
//pending.send();
String title = getString(R.string.app_name);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setStyle(new NotificationCompat.BigTextStyle().bigText("BIG TEXT")).setContentText("CONTENTS").setContentTitle(title);
builder.setContentIntent(pending).setAutoCancel(true);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(1, builder.build());
}
Paste this code:
Intent intent = new Intent(this, ActPending.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), new Random().nextInt(), intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//icon appears in device notification bar and right hand corner of notification
builder.setSmallIcon(R.mipmap.ic_launcher);
// Set the intent that will fire when the user taps the notification.
builder.setContentIntent(pendingIntent);
// Large icon appears on the left of the notification (it accepts Bitmap argument only)
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
// Content title, which appears in large type at the top of the notification
builder.setContentTitle("Notifications Title");
// Content text, which appears in smaller text below the title
builder.setContentText("Your notification content here.");
// The subtext, which appears under the text on newer devices.
// This will show-up in the devices with Android 4.2 and above only
builder.setSubText("Tap to view documentation about notifications.");
//Will clear the notification once it has beeen clicked
builder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(5434, builder.build());
When I receive the notification I would like it to automatically disappear and have to upload manually I, Thank you in advance.
Here I attach the code, in which I create the notification. What I want to happen is that when I receive the notification, I get off, I can see it on the screen, and after a few seconds I go up alone without having to interact with it.
public void notificacion(String label, String autor,String destino) {
NotificationCompat.Builder notifica = new
NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon((((BitmapDrawable) getResources()
.getDrawable(R.drawable.ic_launcher)).getBitmap()))
.setContentTitle("tittle")
.setContentText(label)
.setAutoCancel(true);
Intent intnot = new Intent(this, MainActivity.class);
intnot.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intnot.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intnot.putExtra("Destino", destino);
PendingIntent intnotpend = PendingIntent.getActivity(this, 0, intnot, PendingIntent.FLAG_UPDATE_CURRENT);
notifica.setContentIntent(intnotpend);
notifica.setFullScreenIntent(intnotpend, true);
notifica.setContentIntent(intnotpend);
}
NotificationManager notyman = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notyman.notify(10, notifica.build());
}
am following goolge's guide on how to implement notifications on wearables
Am developing on an emulator, but the damned thing won't take notifications from either another emulator or even physical device .. I need some guidance please.
here's my code
Intent viewIntent = new Intent(getApplicationContext(),
Not.class);
// viewIntent.putExtra(EXTRA_EVENT_ID, eventId);
PendingIntent viewPendingIntent = PendingIntent.getActivity(
getApplicationContext(), 0, viewIntent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
getApplicationContext())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Done").setContentText("In my face")
.setContentIntent(viewPendingIntent);
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager = NotificationManagerCompat
.from(getApplicationContext());
// Build the notification and issues it with notification
// manager.
notificationManager.notify(1,
notificationBuilder.build());
Create mobile app whitout wear.
Then insert this code.
Then run app on handheld emulator. And see the notification on wear emulator. (May be needed to slide down to find it)
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Intent intent = new Intent(this, MyActivity2.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MyActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action action = new NotificationCompat.Action.Builder(
R.drawable.ic_launcher,
getString(R.string.wearTitle),
pendingIntent).build();
Notification notification = new NotificationCompat.Builder(MyActivity.this)
.setContentText("title")
.setContentText("content")
.setSmallIcon(R.drawable.ic_launcher)
.extend(new NotificationCompat.WearableExtender().addAction(action))
.build();
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(001, notification);
}
}
I am working on this app. what i want to do is display the icon of my activity in notification bar on a button click. i want it to stay there permanently until some other event is done. currently i dound this code but its giving a lot of errors, kindly tell me an easy way to do this:
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(icon.isChecked()) {
CharSequence text = getText(R.string.local_service_started);
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.ic_launcher, "Track Your Life", System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.local_service_label), text, contentIntent);
// Send the notification.
mNM.notify(NOTIFICATION, notification);
}
}
});
Simple code for create Notification with icon
Intent intentSetDefault = new Intent(mContext, ConversationListActivity.class);
PendingIntent piSetDeafult = PendingIntent.getActivity(mContext, 0, intentSetDefault,
PendingIntent.FLAG_UPDATE_CURRENT);
// create pending intent for action discard
Intent intentdiscard = new Intent(mContext, NotificationReceiver.class).setType(CODE_DISCARD);
PendingIntent piDiscard = PendingIntent.getBroadcast(mContext, 0, intentdiscard,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder mBuilder =
new Notification.Builder(mContext)
.setTicker(title)
.setSmallIcon(R.drawable.ic_stat_notify_incoming_msg)
.setContentTitle(title)
.setContentText(text)
.setLargeIcon(mAppBitmap)
.setContentInfo(Integer.toString(smsCount))
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(false);
mBuilder.setContentIntent(piSetDeafult);
Notification notification = mBuilder.build();
notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
mNotifyMgr.notify(NOTIFICATION_SET_DEFAULT, notification);
I'm playing around notifications in android, and I'm wondering why NotificationCompat doesn't display Large Icon, and Number in Gingerbread as it does in Jellybean (see pics), I thought that was for that purpose that it was created ?
here is how I fire the notifications :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnShow = (Button)findViewById(R.id.btnNotif);
Intent intent = new Intent(this, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setWhen(System.currentTimeMillis())
.setContentText("You are near your point of interest.")
.setContentTitle("Proximity Alert!")
.setSmallIcon(android.R.drawable.ic_menu_info_details)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.orchide))
.setAutoCancel(true)
.setTicker("Proximity Alert!")
.setNumber(10)
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_VIBRATE| Notification.DEFAULT_SOUND);
/*Create notification with builder*/
notification=notificationBuilder.build();
/*sending notification to system.Here we use unique id (when)for making different each notification
* if we use same id,then first notification replace by the last notification*/
btnShow.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
notificationManager.notify(1000, notification);
}
});
}
Large icon is ignored on pre-honeycomb API levels.
NotificationCompat.Builder documentation says:
...On platform versions that don't offer expanded notifications, methods that depend on expanded notifications have no effect...
If you look at the NotificationCompat.Builder source you'll see that large icon is used for honeycomb and above.