Android notification Java code

I have below code to create notification but it did not work due to some error in NotifyActivity,

public class NotifyActivity extends Activity implements PickerFragment.OnCompleteListener { (...) public void createNotification(Context context){ long when = System.currentTimeMillis(); String []parts; String part1, part2; SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context); status = pref.getString(indiacitys, ""); time = pref.getString(classifiedsindia,""); if (status.compareToIgnoreCase("On")==0) { NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder( context).setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Daily") .setContentText("My first Notification") .setAutoCancel(true).setWhen(when) .setContentIntent(pendingIntent); notificationManager.notify(1,; if(time.compareToIgnoreCase("")!=0) { parts = time.split(":"); part1 = parts[0]; part2 = parts[1]; Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 16);/*TODO: hours, minutes*/ calendar.set(Calendar.MINUTE, 45); calendar.set(Calendar.SECOND, 15); //Service -> MyAlarmService Intent myIntent = new Intent(SchedulerActivity.this, MyReceiver.class); pendingIntent = PendingIntent.getBroadcast(SchedulerActivity.this, 0, myIntent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent); PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0); alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000 , alarmIntent); } } }

I got below error while using above code

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference

A notification is a message you can display to the user outside of your application's normal UI. When you tell the system to issue a notification, it first appears as an icon in the notification area. To see the details of the notification, the user opens the notification drawer. Both the notification area and the notification drawer are system-controlled areas that the user can view at any time.

package com.example.notificationdemo; import; import; import; import android.content.Intent; import; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends ActionBarActivity { Button b1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1=(Button)findViewById(; b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Notify("You've received new message"); } }); } private void Notify(String notificationTitle, String notificationMessage){ NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); @SuppressWarnings("deprecation") Notification notification = new Notification(,"New Message", System.currentTimeMillis()); Intent notificationIntent = new Intent(this,NotificationView.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0); notification.setLatestEventInfo(MainActivity.this, notificationTitle,notificationMessage, pendingIntent); notificationManager.notify(9999, notification); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == { return true; } return super.onOptionsItemSelected(item); } }
Sign In or Register to comment.