Wednesday, April 20, 2016

Create Notification and start new Activity

" Jai Saraswati Maa" 
Hi ... Dear All ..Today I am uploading a superb example of Creating Notification and start of a new activity on click this notification. 
Please check.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box.
Add one button in main layout file with onClick attribute (My function is madeNotification )
 and a display message on second activity layout.
A. Output will be like 
a.     


b.
               


c.    
                



B.  Java Source code 


package com.exam.ravi.notificationex1;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

    }
    public void madeNotification(View view) {

        Intent intent = new Intent(this, NotificationReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent,
                                               PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle("New mail from  Ravi Sir");
        builder.setContentText("Hi this is notification mail");
        builder .setSmallIcon(R.mipmap.ic_launcher);
        builder .setContentIntent(pendingIntent);
        Notification notification = builder.build();
        NotificationManager notificationManager = (NotificationManager) 
                                 getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(1, notification);
    }
} 

No comments:

Post a Comment