Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Hands-up Notification Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.01k
    Comment on it

    Hand-Up notification comes up with API level 21.
    Hands-up notification is open small floating window on top of the screen only when device is active.

    Hand-up notifications are High-priority notifications.Hands-up notification required to set a vibrate or ring-tone to make Heads-up work.

    Hands-up notification are uses like alarm,incoming call,messages,battery low. etc.

    Lets start with code:-

    1. Create xml file of your activity like below.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.android.basicnotifications.HandsUpActivity">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Notification demo for Hands Up Notification"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="131dp" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show Notification"
            android:onClick="showNotification"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />
    
    </RelativeLayout>
    

    2. Open Java file of Activity.

    3. Create showNotification method of button view.

    public void showNotification(View view){}

    4. Create Notification Builder Instance.

     NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    5. Set Content text and small icon.

     builder.setContentText("Alarm");
    
            builder.setSmallIcon(R.drawable.ic_launcher);
    

    6. Create Dismiss Intent and pending intent.

     Intent dismissIntent = new Intent(this, MainActivity.class);
    
            PendingIntent piDismiss = PendingIntent.getActivity(this, 0, dismissIntent, 0);

    7. Set Snooze Intent and pending intent.

     Intent snoozeIntent = new Intent(this, MainActivity.class);
            PendingIntent piSnooze = PendingIntent.getActivity(this, 0, snoozeIntent, 0);

    8. Set Defaults,priority and action.

     builder.setDefaults(Notification.DEFAULT_ALL) // must requires VIBRATE permission
                    .setPriority(NotificationCompat.PRIORITY_HIGH) //must have priority to High, Max
                    .addAction(R.drawable.ic_launcher,
                            getString(R.string.dismiss), piDismiss)
                    .addAction(R.drawable.ic_launcher,
                            getString(R.string.snooze), piSnooze);

    9. Create notificationManager instance.

     NotificationManager notificationManager = (NotificationManager) getSystemService(
                    NOTIFICATION_SERVICE);
           

    10. Notify Notification.

     notificationManager.notify(11, builder.build());

    Happy Coding :D

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: