Skip to content

Most visited

Recently visited

navigation
LNotifications / src / com.example.android.lnotifications /

HeadsUpNotificationFragment.java

1
/*
2
* Copyright 2014 The Android Open Source Project
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
*     http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
 
17
package com.example.android.lnotifications;
18
 
19
import android.app.Notification;
20
import android.app.NotificationManager;
21
import android.app.PendingIntent;
22
import android.content.Context;
23
import android.content.Intent;
24
import android.os.Bundle;
25
import android.support.v4.app.Fragment;
26
import android.view.LayoutInflater;
27
import android.view.View;
28
import android.view.ViewGroup;
29
import android.widget.Button;
30
import android.widget.CheckBox;
31
import android.widget.Toast;
32
 
33
/**
34
 * Fragment that demonstrates options for displaying Heads-Up Notifications.
35
 */
36
public class HeadsUpNotificationFragment extends Fragment {
37
 
38
    /**
39
     * NotificationId used for the notifications from this Fragment.
40
     */
41
    private static final int NOTIFICATION_ID = 1;
42
 
43
    private NotificationManager mNotificationManager;
44
 
45
    /**
46
     * Button to show a notification.
47
     */
48
    private Button mShowNotificationButton;
49
 
50
    /**
51
     * If checked, notifications that this Fragment creates will be displayed as Heads-Up
52
     * Notifications.
53
     */
54
    private CheckBox mUseHeadsUpCheckbox;
55
 
56
    /**
57
     * Use this factory method to create a new instance of
58
     * this fragment using the provided parameters.
59
     *
60
     * @return A new instance of fragment NotificationFragment.
61
     */
62
    public static HeadsUpNotificationFragment newInstance() {
63
        HeadsUpNotificationFragment fragment = new HeadsUpNotificationFragment();
64
        fragment.setRetainInstance(true);
65
        return fragment;
66
    }
67
 
68
    public HeadsUpNotificationFragment() {
69
        // Required empty public constructor
70
    }
71
 
72
    @Override
73
    public void onCreate(Bundle savedInstanceState) {
74
        super.onCreate(savedInstanceState);
75
        mNotificationManager = (NotificationManager) getActivity().getSystemService(Context
76
                .NOTIFICATION_SERVICE);
77
    }
78
 
79
    @Override
80
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
81
                             Bundle savedInstanceState) {
82
        // Inflate the layout for this fragment
83
        return inflater.inflate(R.layout.fragment_heads_up_notification, container, false);
84
    }
85
 
86
    @Override
87
    public void onViewCreated(View view, Bundle savedInstanceState) {
88
        super.onViewCreated(view, savedInstanceState);
89
        mShowNotificationButton = (Button) view.findViewById(R.id.show_notification_button);
90
        mShowNotificationButton.setOnClickListener(new View.OnClickListener() {
91
            @Override
92
            public void onClick(View view) {
93
                mNotificationManager.notify(NOTIFICATION_ID, createNotification(
94
                        mUseHeadsUpCheckbox.isChecked()));
95
                Toast.makeText(getActivity(), "Show Notification clicked", Toast.LENGTH_SHORT).show();
96
            }
97
        });
98
        mUseHeadsUpCheckbox = (CheckBox) view.findViewById(R.id.use_heads_up_checkbox);
99
    }
100
 
101
    /**
102
     * Creates a new notification depending on the argument.
103
     *
104
     * @param makeHeadsUpNotification A boolean value to indicating whether a notification will be
105
     *                                created as a heads-up notification or not.
106
     *                                <ul>
107
     *                                <li>true : Creates a heads-up notification.</li>
108
     *                                <li>false : Creates a non-heads-up notification.</li>
109
     *                                </ul>
110
     *
111
     * @return A Notification instance.
112
     */
113
    private Notification createNotification(boolean makeHeadsUpNotification) {
114
        Notification.Builder notificationBuilder = new Notification.Builder(getActivity())
115
                .setSmallIcon(R.drawable.ic_launcher_notification)
116
                .setPriority(Notification.PRIORITY_DEFAULT)
117
                .setCategory(Notification.CATEGORY_MESSAGE)
118
                .setContentTitle("Sample Notification")
119
                .setContentText("This is a normal notification.");
120
        if (makeHeadsUpNotification) {
121
            Intent push = new Intent();
122
            push.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
123
            push.setClass(getActivity(), LNotificationActivity.class);
124
 
125
            PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(getActivity(), 0,
126
                    push, PendingIntent.FLAG_CANCEL_CURRENT);
127
            notificationBuilder
128
                    .setContentText("Heads-Up Notification on Android L or above.")
129
                    .setFullScreenIntent(fullScreenPendingIntent, true);
130
        }
131
        return notificationBuilder.build();
132
    }
133
}
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.