Monday, November 21, 2016

Active Notifications in Android 7 Example Code



<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout  
xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent">
    <LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content">
        <Button 
 android:id="@+id/add_notification" 
 android:text="Add a notification" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" />
    </LinearLayout>

    <TextView 
 style="@android:style/TextAppearance.Material.Large" 
 android:id="@+id/number_of_notifications" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" />
 </LinearLayout>
 

public class MainActivity extends AppCompatActivity {
    String TAG = "MainActivity";
    private static final int REQUEST_CODE = 2323;
    private TextView mNumberOfNotifications;
    private PendingIntent mDeletePendingIntent;
    protected static final String ACTION_NOTIFICATION_DELETE 
 = "com.example.android.activenotifications.delete";
    private static final String NOTIFICATION_GROUP =
            "com.example.android.activenotifications.notification_type";
    private NotificationManager mNotificationManager;
    private static final int NOTIFICATION_GROUP_SUMMARY_ID = 1;
    private static int sNotificationId = NOTIFICATION_GROUP_SUMMARY_ID + 1;

    @Override 
 protected void onResume() {
        super.onResume();
        updateNumberOfNotifications();
    }

    @SuppressLint("StringFormatMatches")
    protected void updateNumberOfNotifications() {
        final StatusBarNotification[] activeNotifications = mNotificationManager                .getActiveNotifications();
        final int numberOfNotifications = activeNotifications.length;
        mNumberOfNotifications.setText(getString(R.string.active_notifications,
                numberOfNotifications));
        Log.i(TAG, getString(R.string.active_notifications, numberOfNotifications));
    }

    protected void updateNotificationSummary() {
        final StatusBarNotification[] activeNotifications = mNotificationManager                .getActiveNotifications();

        int numberOfNotifications = activeNotifications.length;
        for (StatusBarNotification notification : activeNotifications) {
            if (notification.getId() == NOTIFICATION_GROUP_SUMMARY_ID) {
                numberOfNotifications--;
                break;
            }
        }

        if (numberOfNotifications > 1) {
            // There are %s ActiveNotifications 
 String notificationContent = getString(R.string.sample_notification_summary_content,
                    "" + numberOfNotifications);
            final NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .setSummaryText(notificationContent))
                    .setGroup(NOTIFICATION_GROUP)
                    .setGroupSummary(true);
            final Notification notification = builder.build();
            mNotificationManager.notify(NOTIFICATION_GROUP_SUMMARY_ID, notification);
        } else {
            mNotificationManager.cancel(NOTIFICATION_GROUP_SUMMARY_ID);
        }
    }

    private void addNotificationAndUpdateSummaries() {
        final NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(getString(R.string.app_name))
                .setContentText("This is a sample notification")
                .setAutoCancel(true)
                .setDeleteIntent(mDeletePendingIntent)
                .setGroup(NOTIFICATION_GROUP);

        final Notification notification = builder.build();
        mNotificationManager.notify(getNewNotificationId(), notification);
        Log.d(TAG, "Add a notification");

        updateNotificationSummary();
        updateNumberOfNotifications();
    }

    public int getNewNotificationId() {
        int notificationId = sNotificationId++;
        if (notificationId == NOTIFICATION_GROUP_SUMMARY_ID) {
            notificationId = sNotificationId++;
        }
        return notificationId;
    }

    void init() {
        mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(
                Context.NOTIFICATION_SERVICE);
        mNumberOfNotifications = (TextView) findViewById(R.id.number_of_notifications);

        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override 
 public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.add_notification: {
                        addNotificationAndUpdateSummaries();
                        break;
                    }
                }
            }
        };
        findViewById(R.id.add_notification).setOnClickListener(onClickListener);
        Intent deleteIntent = new Intent(ACTION_NOTIFICATION_DELETE);
        mDeletePendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
                REQUEST_CODE, deleteIntent, 0);
    }

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


        init();
    }

    @Override 
 public void onBackPressed() {
        super.onBackPressed();
    }


}

No comments:

Post a Comment