Showing posts with label Notification. Show all posts
Showing posts with label Notification. Show all posts

Wednesday, April 20, 2016

Customized Notification - More Powerful

" Jai Saraswati Maa" 
Hi ... Dear All ..Today I am uploading a superb example of Creating Notification with Action and after click it will disappear. 
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.
**** Nothing in layout xml file its blank
A. Output will be like 
a.      


b.   
          


B. Java 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);
 Intent intent = new Intent(Intent.ACTION_DIAL);

    PendingIntent pendingIntent = PendingIntent.getActivity(this,1,intent,0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle("Lunch is ready! ");
    builder.setContentText("Call Him...It's getting cold...");
    builder.setContentIntent(pendingIntent);
    builder .addAction(android.R.drawable.ic_menu_call, "Call", pendingIntent);
    builder.setSmallIcon(android.R.drawable.ic_menu_call);
    builder.setAutoCancel(true);
    Notification notification = builder.build();
    NotificationManager notificationManager = (NotificationManager) 
                                    getSystemService(NOTIFICATION_SERVICE);

    notificationManager.notify(1,notification);
    }
}


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);
    }
}