Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.teacher.mysnackbarnotification;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Color;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.support.v7.app.NotificationCompat;
- import android.view.View;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- Context context;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- this.context = this;
- }
- public void onClick(View view) {
- String message = "רק היום, מבצע פיצוץ, 8 קילו לפרצוף";
- int myDuration = 5000;
- Snackbar.make(view, message, Snackbar.LENGTH_LONG) //create the snack bar like we create Toast
- .setAction("הבנתי כפרה", new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Toast.makeText(MainActivity.this, "תומר בדרך אלייך", Toast.LENGTH_LONG).show();
- }
- })
- .setActionTextColor(Color.YELLOW) //set the color!!!
- .setDuration(myDuration) //set the duration to display
- .show(); //show the snack bar
- }
- private void createNotification(int nId, //Notification id
- int iconRes, //icon resource from R file
- String title, //notification title
- String body) //notification body
- {
- NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) //createing to android support.v7.app.NotificationComapt
- new NotificationCompat.Builder (context)
- .setSmallIcon(iconRes)
- .setContentTitle(title)
- .setContentText(body);
- NotificationManager myNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- //nId -> notification id -> allow us to update the notification later on
- myNotificationManager.notify(nId,notificationBuilder.build());
- }
- private void createNotificationWithAction(int nId,int iconRes, String title, String body)
- {
- //first lets define the intent to triger when notifcation is selected
- //start out by creating a normal intent (but we can use the present also a fragment)
- Intent intent=new Intent(context,Main2Activity.class);
- //next, let's turn this into a pending intent
- //using: public static pandingIntent getActivity(Context context, int requestCode, Intent intent, int flag)
- int requestId=(int)System.currentTimeMillis(); //unique request id to diff between various notification with same notification message
- int flags = PendingIntent.FLAG_CANCEL_CURRENT; //cancel an old intent to create a new one
- PendingIntent pendingIntent=PendingIntent.getActivity(this,requestId,intent,flags);
- //now we can attach the pending intent to a new notication use set content intent
- Notification noti=new NotificationCompat.Builder(this)
- .setSmallIcon(iconRes)
- .setContentTitle(title)
- .setContentText(body)
- .setContentIntent(pendingIntent)
- .addAction(R.drawable.thumb_up, "רוצה קונה", pendingIntent)
- .addAction(R.drawable.thumb_down, "לא רוצה", pendingIntent)
- .setAutoCancel(true) //hides the notification after it been selected
- .build();
- //get the notification managar system service
- NotificationManager myNotificationManagar = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
- //Nid allows us to update the notification later on......
- myNotificationManagar.notify(nId,noti);
- }
- public void simpleNotification(View view)
- {
- createNotification(69,R.drawable.gas,"מבצע של מאוס גאוס", "רק היום, מבצע פיצוץ, 8 קילו לפרצוף");
- }
- public void zeevikNotification(View view)
- {
- createNotificationWithAction(43,R.drawable.gas,"גז במחיר פגז","הזמן עכשיו, תומר בשטח כולם במתח (במקלטים)");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement