카테고리 없음

[기계적 인조 인간] Parse 라이브러리를 사용하여 알림에 큰보기 스타일을 적용하는 방법

행복을전해요 2021. 2. 18. 02:16

다음은 Notification.BigTextStyle.

    final String someLongText = "fkdljfdldkfj;ldaksjfkladj;flja;lkjdfljadslfjaddfdsfafjdfad" +
            "fdl;akjf;lkdf;lkaj;flkjda;lkfjadljflk;adsjfladjflk;dfjlkdjflakdfjdaffjdlfjdjjj" +
                        "adjflkjadlkfjad;lkfjad;sljf;ladkjajlkfjad;lksfjl;akdjf;lkdsajf;lkdjfkadj;flkad" +
                                    "jf;lkadjfkldas;lkfja;dljf;lkdasjf;lkadjs;lfjas;ldkfj;lkadsjfl;kadljfl;kasdjf;l" +
                                                "jdlskfjklda;fjadslkfj;sdalkfj;ladjf;lajdl;fkajld;kfjlajfl;adjfl;kajdl;fjadl;kfj;";
                                                
                                                    final Notification.Builder builder = new Notification.Builder(this);
                                                        builder.setStyle(new Notification.BigTextStyle(builder)
                                                                    .bigText(someLongText)
                                                                                .setBigContentTitle("Big title")
                                                                                            .setSummaryText("Big summary"))
                                                                                                        .setContentTitle("Title")
                                                                                                                    .setContentText("Summary")
                                                                                                                                .setSmallIcon(android.R.drawable.sym_def_app_icon);
                                                                                                                                
                                                                                                                                    final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                                                                                                                                        nm.notify(0, builder.build());
                                                                                                                                        

예

-------------------
Bitmap icon1 = BitmapFactory.decodeResource(getResources(),
                    R.drawable.gorio);
                    
                                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                                                    getApplicationContext()).setAutoCancel(true)
                                                                        .setContentTitle("Exemplo 1")
                                                                                            .setSmallIcon(R.drawable.gorio)
                                                                                                                .setLargeIcon(icon1).setContentText("Hello World!");
                                                                                                                
                                                                                                                            NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
                                                                                                                                        bigText.bigText(msg);
                                                                                                                                                    bigText.setBigContentTitle("GORIO Engenharia");
                                                                                                                                                                bigText.setSummaryText("Por: GORIO Engenharia");
                                                                                                                                                                            mBuilder.setStyle(bigText);
                                                                                                                                                                                        mBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
                                                                                                                                                                                        
                                                                                                                                                                                                    // Creates an explicit intent for an Activity in your app
                                                                                                                                                                                                                Intent resultIntent = new Intent(getApplicationContext(),
                                                                                                                                                                                                                                    MainActivity.class);
                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                // The stack builder object will contain an artificial back
                                                                                                                                                                                                                                                            // stack for
                                                                                                                                                                                                                                                                        // the
                                                                                                                                                                                                                                                                                    // started Activity.
                                                                                                                                                                                                                                                                                                // getApplicationContext() ensures that navigating backward from
                                                                                                                                                                                                                                                                                                            // the Activity leads out of
                                                                                                                                                                                                                                                                                                                        // your application to the Home screen.
                                                                                                                                                                                                                                                                                                                                    TaskStackBuilder stackBuilder = TaskStackBuilder
                                                                                                                                                                                                                                                                                                                                                        .create(getApplicationContext());
                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                    // Adds the back stack for the Intent (but not the Intent
                                                                                                                                                                                                                                                                                                                                                                                // itself)
                                                                                                                                                                                                                                                                                                                                                                                            stackBuilder.addParentStack(MainActivity.class);
                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                        // Adds the Intent that starts the Activity to the top of the
                                                                                                                                                                                                                                                                                                                                                                                                                    // stack
                                                                                                                                                                                                                                                                                                                                                                                                                                stackBuilder.addNextIntent(resultIntent);
                                                                                                                                                                                                                                                                                                                                                                                                                                            PendingIntent resultPendingIntent = stackBuilder
                                                                                                                                                                                                                                                                                                                                                                                                                                                                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            mBuilder.setContentIntent(resultPendingIntent);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    // mId allows you to update the notification later on.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                mNotificationManager.notify(100, mBuilder.build());
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
-------------------

이 스 니펫은 Builder 객체 를 생성하는 방법을 보여줍니다 . 큰보기의 스타일을 큰 텍스트 로 설정하고 내용을 미리 알림 메시지로 설정합니다.

String msg="This is Big style notification builder.This is Big style notification  builder.This is Big style notification builder.This is Big style notification builder.This is Big style notification builder.This is Big style notification builder.This is Big style notification builder.This is Big style notification builder.This is Big style notification builder.This is Big style notification builder."

// Constructs the Builder object.
NotificationCompat.Builder builder =
    new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_stat_notification)
            .setContentTitle(getString(R.string.notification))
                .setContentText(getString(R.string.ping))
                    .setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                        
                        final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                        nm.notify(0, builder.build());
                        

자세한 정보는 http://developer.android.com/training/notify-user/expanded.html 로 이동 하십시오.

-------------------

여기에 스크린 샷을 첨부했고, 첫 화면 내용은 게시물 제목이고 앱 이름 오른쪽에있는 아래쪽 화살표를 클릭하면 푸시 알림을위한 맞춤 레이아웃 인 두 번째 스크린 샷으로 연결됩니다. 다음은 샘플 레이아웃입니다. 제가 디자인 한 것입니다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true">

<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#80000000"
android:layout_height="wrap_content">

<ImageView
    android:src="@mipmap/ic_launcher"
        android:layout_width="50dp"
            android:layout_height="match_parent"
                android:padding="10dp"
                    android:layout_marginLeft="5dp"
                        android:background="@null"
                            android:layout_gravity="center_vertical|center_horizontal"
                                android:scaleType="centerCrop"/>
                                <TextView
                                    android:id="@+id/title"
                                        android:layout_width="match_parent"
                                            android:layout_height="wrap_content"
                                                android:layout_gravity="bottom"
                                                    android:minHeight="48dp"
                                                        android:paddingBottom="16dp"
                                                            android:paddingLeft="16dp"
                                                                android:paddingRight="16dp"
                                                                    android:paddingTop="16dp"
                                                                        android:background="@android:color/transparent"
                                                                            android:textColor="@android:color/white"
                                                                                tools:text="Test"/>
                                                                                 </LinearLayout>
                                                                                  <ImageView
                                                                                  android:id="@+id/image"
                                                                                  android:layout_width="match_parent"
                                                                                  android:layout_height="fill_parent"
                                                                                  android:adjustViewBounds="true"
                                                                                  android:contentDescription="@null"
                                                                                  android:scaleType="centerCrop"
                                                                                  android:src="@drawable/placeholder"/>
                                                                                  </LinearLayout>
                                                                                  

사용자 지정 레이아웃으로 알림을 생성하는 방법,

public static void createNotification(String title, String body,String image_url, Context context, int notificationsId, String single_id) {
Intent notificationIntent;

long when = System.currentTimeMillis();
int id = (int) System.currentTimeMillis();

Bitmap bitmap = getBitmapFromURL(image_url);
NotificationCompat.BigPictureStyle notifystyle = new NotificationCompat.BigPictureStyle();
notifystyle.bigPicture(bitmap);
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);
contentView.setImageViewBitmap(R.id.image, bitmap);
contentView.setTextViewText(R.id.title, body);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.ic_launcher)
                .setStyle(notifystyle)
                        .setCustomBigContentView(contentView)
                                .setContentText(body);
                                NotificationManager mNotificationManager = (NotificationManager) context
                                        .getSystemService(Context.NOTIFICATION_SERVICE);
                                        
                                        notificationIntent = new Intent(context, SinglePost.class);
                                        notificationIntent.putExtra("single_id",single_id);
                                        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                                        PendingIntent contentIntent = PendingIntent.getActivity(context, id, notificationIntent, 0); 
                                        
                                        Notification notification = mBuilder.build();
                                        notification.contentIntent = contentIntent;
                                        notification.flags |= Notification.FLAG_AUTO_CANCEL;
                                        notification.defaults |= Notification.DEFAULT_SOUND;
                                        notification.defaults |= Notification.DEFAULT_VIBRATE;
                                        
                                        
                                        mNotificationManager.notify(notificationsId, notification);
                                        
                                        }
                                        
                                        public static Bitmap getBitmapFromURL(String strURL) {
                                        try {
                                            URL url = new URL(strURL);
                                                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                                                    connection.setDoInput(true);
                                                        connection.connect();
                                                            InputStream input = connection.getInputStream();
                                                                Bitmap myBitmap = BitmapFactory.decodeStream(input);
                                                                    return myBitmap;
                                                                    } catch (IOException e) {
                                                                        e.printStackTrace();
                                                                            return null;
                                                                            }
                                                                            }
                                                                            

여기에 이미지 설명 입력

여기에 이미지 설명 입력

-------------------

당신은 설정

String en_alert ,th_alert ,en_title,th_title ,id;
int noti_all, noti_1, noti_2, noti_3, noti_4 = 0, Langage;

Intent intent = new Intent(context, ReserveStatusActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

 NotificationManager notificationManager =
       (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
       
       intent = new Intent(String.valueOf(PushActivity.class));
       intent.putExtra("message", MESSAGE);
       TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
       stackBuilder.addParentStack(PushActivity.class);
       stackBuilder.addNextIntent(intent);
       // PendingIntent pendingIntent =
       stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
       
       // android.support.v4.app.NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
       // bigStyle.bigText((CharSequence) context);
       
       notification = new NotificationCompat.Builder(context)
                        .setSmallIcon(R.mipmap.ic_launcher)
                                         .setContentTitle(th_title ) // th_title  >>> String =  th_title   
                                                          .setContentText(th_alert)   // th_alert>>> String =  th_title
                                                                           .setAutoCancel(true)
                                                                                            //.setStyle(new Notification.BigTextStyle().bigText(th_alert)
                                                                                                            .setStyle(new NotificationCompat.BigTextStyle().bigText(th_title))
                                                                                                                            .setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))
                                                                                                                                            .setContentIntent(pendingIntent)
                                                                                                                                                            .setNumber(++numMessages)
                                                                                                                                                                            .build();
                                                                                                                                                                            
                                                                                                                                                                            notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                                                                                                                                                                            
                                                                                                                                                                            notificationManager.notify(1000, notification);
                                                                                                                                                                            

페이스 북 facebook.com/PongLoveWii

-------------------
final String someLongText = "......";

final Notification.Builder builder = new Notification.Builder(this);
builder.setStyle(new Notification.BigTextStyle(builder)
        .bigText(someLongText)
                .setBigContentTitle("Big title")
                        .setSummaryText("Big summary"))
                                .setContentTitle("Title")
                                        .setContentText("Summary");
                                        
                                        final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                                        nm.notify(0, builder.build());
                                        
-------------------

큰 텍스트 스타일로 알림을 만들 수 있습니다.

 Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                    PendingIntent.FLAG_ONE_SHOT);
                    
                        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                                        .setSmallIcon(R.mipmap.ic_launcher)
                                                    .setContentTitle(title)
                                                                .setContentText(messageBody)
                                                                            .setAutoCancel(true)
                                                                                        .setSound(defaultSoundUri)
                                                                                                    .setContentIntent(pendingIntent);
                                                                                                    
                                                                                                        NotificationManager notificationManager =
                                                                                                                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                                                                                                                    
                                                                                                                        notificationManager.notify(id /* ID of notification */, notificationBuilder.build());
                                                                                                                        
-------------------

ParsePushBroadcastReceiver 하위 클래스의 getNotification 메서드를 재정의해야하며 위에는 알림에 큰 콘텐츠보기를 설정하는 방법에 대한 많은 답변이 있습니다.



출처
https://stackoverflow.com/questions/22079840