如何在安卓系统中制作振动通知?

问题描述 投票:0回答:1

我是Android新手,面临着创建振动通知的问题。

这两种方法都不行。

AndroidManifest.xml:

<uses-permission android:name="android.permission.VIBRATE" />

然后在我的模块中

            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "channel",
                    NotificationManager.IMPORTANCE_HIGH);
            channel.setDescription("channel description");
            channel.enableVibration(true);
            channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            manager.createNotificationChannel(channel);

第二种方法

            builder = new NotificationCompat.Builder(context, CHANNEL_ID);
        } else {
            builder = new NotificationCompat.Builder(context);
        }

        Intent activityIntent = new Intent(context, MainActivity.class);
        activityIntent.putExtra("FromNotification", true);
        PendingIntent action = PendingIntent.getActivity(context, 0, activityIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        builder.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_stat_icon_))
                .setSmallIcon(R.drawable.ic_stat_icon_).setTicker("Large text!").setAutoCancel(true)
                 .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000})
                .setContentTitle(notificationMessage).setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_CALL).setContentText(contentText)
                .setFullScreenIntent(action, true);

我应该如何修改代码才能使之工作?

android notifications
1个回答
0
投票

这对我有用,你可以试试

//----------------------------------

 protected void displayNotification() {

        Log.i("Start", "notification");

      // Invoking the default notification service //
        NotificationCompat.Builder  mBuilder =
                new NotificationCompat.Builder(this);
        mBuilder.setAutoCancel(true);

        mBuilder.setContentTitle("New Message");
        mBuilder.setContentText("You have "+unMber_unRead_sms +" new message.");
        mBuilder.setTicker("New message from PayMe..");
        mBuilder.setSmallIcon(R.drawable.icon2);

      // Increase notification number every time a new notification arrives //
        mBuilder.setNumber(unMber_unRead_sms);

      // Creates an explicit intent for an Activity in your app //

        Intent resultIntent = new Intent(this, FreesmsLog.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(FreesmsLog.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);

      //  mBuilder.setOngoing(true);
        Notification note = mBuilder.build();
        note.defaults |= Notification.DEFAULT_VIBRATE;
        note.defaults |= Notification.DEFAULT_SOUND;

        mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      // notificationID allows you to update the notification later on. //
        mNotificationManager.notify(notificationID, mBuilder.build());

    }

0
投票

对我有用的是

activity_main. xml

<? xml version= "1.0" encoding= "utf-8" ?>
<android.support.constraint.ConstraintLayout
   xmlns: android = "http://schemas.android.com/apk/res/android"
   xmlns: app = "http://schemas.android.com/apk/res-auto"
   xmlns: tools = "http://schemas.android.com/tools"
   android :layout_width= "match_parent"
   android :layout_height= "match_parent"
   android :padding= "16dp"
   tools :context= ".MainActivity" >
   <Button
      android :id = "@+id/btnCreateNotification"
      android :layout_width= "0dp"
      android :layout_height= "wrap_content"
      android :text = "Create notification"
      app :layout_constraintBottom_toBottomOf= "parent"
      app :layout_constraintEnd_toEndOf= "parent"
      app :layout_constraintStart_toStartOf= "parent"
      app :layout_constraintTop_toTopOf= "parent" />
</android.support.constraint.ConstraintLayout>

主要活动

public class MainActivity extends AppCompatActivity {
   private final static String default_notification_channel_id = "default";
   @Override
   protected void onCreate (Bundle savedInstanceState) {
      super .onCreate(savedInstanceState);
      setContentView(R.layout. activity_main );
      Button btnCreateNotification = findViewById(R.id. btnCreateNotification );
      btnCreateNotification.setOnClickListener( new View.OnClickListener() {
         @Override
         public void onClick (View v) {
            Intent intent = new Intent(getApplicationContext(), MainActivity. class );
            final PendingIntent resultPendingIntent =
            PendingIntent. getActivity (
               MainActivity. this,
               0,
               intent,
               PendingIntent. FLAG_CANCEL_CURRENT
            );
            NotificationCompat.Builder mBuilder =
               new NotificationCompat.Builder(MainActivity. this,
               default_notification_channel_id )
               .setSmallIcon(R.drawable. ic_launcher_foreground )
               .setContentTitle( "Test" )
               .addAction(R.drawable. ic_launcher_foreground, "Add", resultPendingIntent)
               .setContentIntent(resultPendingIntent)
               .setStyle( new NotificationCompat.BigTextStyle().bigText( "Big View Styles" ))
               .setContentText( "Hello! This is my first push notification" );
            NotificationManager mNotificationManager = (NotificationManager)
               getSystemService(Context. NOTIFICATION_SERVICE );
            mNotificationManager.notify(( int ) System. currentTimeMillis (), mBuilder.build()) ;
         }
      });
   }
}

在你的宣言中

<uses-permission android :name= "android.permission.VIBRATE" />

结果如你所见

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.