Android
 Computer >> コンピューター >  >> プログラミング >> Android

Androidで通知チャネルを作成および管理する


この例は、Androidでの通知チャネルの作成と管理について示しています

ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。

ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。

<? xml version= "1.0" encoding= "utf-8" ?>
<android.support.constraint.ConstraintLayout
   xmlns: android = "https://schemas.android.com/apk/res/android"
   xmlns: app = "https://schemas.android.com/apk/res-auto"
   xmlns: tools = "https://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>

ステップ3 −rawフォルダにサウンドを追加する

Androidで通知チャネルを作成および管理する

ステップ4 −次のコードをsrc / MainActivity.java

に追加します
package app.tutorialspoint.com.notifyme ;
import android.app.NotificationChannel ;
import android.app.NotificationManager ;
import android.content.ContentResolver ;
import android.content.Context ;
import android.graphics.Color ;
import android.media.AudioAttributes ;
import android.net.Uri ;
import android.support.v4.app.NotificationCompat ;
import android.support.v7.app.AppCompatActivity ;
import android.os.Bundle ;
import android.view.View ;
import android.widget.Button ;
public class MainActivity extends AppCompatActivity {
   public static final String NOTIFICATION_CHANNEL_ID = "10001" ;
   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) {
            Uri sound = Uri. parse (ContentResolver. SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/quite_impressed.mp3" ) ;
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity. this,
               default_notification_channel_id )
               .setSmallIcon(R.drawable. ic_launcher_foreground )
               .setContentTitle( "Test" )
               .setSound(sound)
               .setContentText( "Hello! This is my first push notification" );
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context. NOTIFICATION_SERVICE ) ;
            if (android.os.Build.VERSION. SDK_INT >= android.os.Build.VERSION_CODES. O ) {
               AudioAttributes audioAttributes = new AudioAttributes.Builder()
                  .setContentType(AudioAttributes. CONTENT_TYPE_SONIFICATION )
                  .setUsage(AudioAttributes. USAGE_ALARM )
                  .build() ;
               int importance = NotificationManager. IMPORTANCE_HIGH ;
               NotificationChannel notificationChannel = new
                  NotificationChannel( NOTIFICATION_CHANNEL_ID , "NOTIFICATION_CHANNEL_NAME" , importance) ;
               notificationChannel.enableLights( true ) ;
               notificationChannel.setLightColor(Color. RED ) ;
               notificationChannel.enableVibration( true ) ;
               notificationChannel.setVibrationPattern( new long []{ 100 , 200 , 300 , 400 , 500 , 400 , 300 , 200 , 400 }) ;
               notificationChannel.setSound(sound , audioAttributes) ;
               mBuilder.setChannelId( NOTIFICATION_CHANNEL_ID ) ;
               assert mNotificationManager != null;
               mNotificationManager.createNotificationChannel(notificationChannel) ;
            }
            assert mNotificationManager != null;
            mNotificationManager.notify(( int ) System. currentTimeMillis (), mBuilder.build()) ;
         }
      }) ;
   }
}

ステップ5 −次のコードをandroidManifest.xmlに追加します

<? xml version = "1.0" encoding = "utf-8" ?>
<manifest xmlns: android = "https://schemas.android.com/apk/res/android"
   package = "app.tutorialspoint.com.notifyme" >
   <application
      android :allowBackup = "true"
      android :icon = "@mipmap/ic_launcher"
      android :label = "@string/app_name"
      android :roundIcon = "@mipmap/ic_launcher_round"
      android :supportsRtl = "true"
      android :theme = "@style/AppTheme" >
      <activity android :name = ".MainActivity" >
         <intent-filter>
            <action android :name = "android.intent.action.MAIN" />
            <category android :name = "android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 android studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[実行]アイコンをクリックします。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します-

Androidで通知チャネルを作成および管理する


  1. iPhone で連絡先グループを作成および管理する方法

    連絡先と頻繁に通信する必要がある場合は、連絡先を効率的に管理する方法を見つける必要があります。定期的に個人ではなくグループと通信する場合、連絡先グループを設定することは、連絡先を管理するための優れた方法です。 連絡先をより効率的に整理するのに役立つだけでなく、必要なときに簡単に連絡を取ることができます.この記事では、iPhone で連絡先グループを作成および管理する方法を紹介します。 集まりやグループでの休暇を設定するために 1 人の人に個別にメッセージを送信すると、かなりの時間がかかる可能性があり、iOS は iPhone でグループを作成する簡単な方法を提供しません。 幸いなことに、

  2. Android でのデータ使用状況を管理および追跡する方法

    なんらかの質問があるたびに、スマートフォンのロックを解除し、回答や解決策を求めて Google に問い合わせます。これだけでなく、オンライン取引、ビデオ通話による友人とのコミュニケーション、タクシーの予約、オンライン ショッピング、またはお気に入りのビデオの視聴にもスマートフォンを使用しています。しかし、これらすべてのアクティビティはモバイル データに多大な負担をかけ、多くの場合、電話代が膨れ上がります。したがって、ユーザーがデータの使用状況を注意深く監視することが非常に重要です。 これには、データ追跡アプリを利用することもできます。 Google Play には、データの使用量を管理および監