Tuesday, April 9, 2013

[android help] notification method NotificationCompat - no errors - no notification

android - notification method NotificationCompat - no errors - no notification - Stack Overflow



















I am trying to get a simple notification. I've seen lots of examples using old API methods, but i want to use the newer method. I have put this code together from the API resource, and it runs without error. However, i am not seeing any notifications. I have the minimum required to view a notification, at least, i believe so from what i have read. Here is my code :



public class Login extends Activity {
public static Context ctx;

public void onCreate(Bundle savedInstanceState) {
ctx = this;
}

private static void notice(String msgfrom, String msg) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx);
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setContentTitle("New message from " + msgfrom.toString());
mBuilder.setContentText(msg);
mBuilder.build();
}
}

























Try this


Just call inside your onCreate()



Public void Player_Notification()
{
final String myBlog = "http://android-er.blogspot.com/";
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = new Notification(R.drawable.ic_launcher,"Audio Track is playing in background!",System.currentTimeMillis());
Context context = getApplicationContext();

// here is title
String notificationTitle = Title;
// here is sub title
String notificationText = data;
// write your activity name which you want to open when user click on notification
Intent myIntent = new Intent(this, Activty.class);
PendingIntent pendingIntent = PendingIntent.getActivity(Activty.this, 0, myIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);

// intent will call your activity as you want
myNotification.defaults |= Notification.DEFAULT_SOUND;
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.setLatestEventInfo(context, notificationTitle,
notificationText, pendingIntent);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

}






















enter image description here


If you want to run Notification in All Android device <= android 4.2 then just add android-support-v4.jar file in you Project and


use below code:



NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());



















default






.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...