2017-11-09 1 views
1

これは、ボタンをクリックして通知を作成し、別のクラスに切り替えるコードです。 2番目のクラスは完全に空です。なぜ他のクラスに向けることができないのかわからない。Android-Notificationが表示されない

私はすでにこのファイルの「着席」をチェックしており、「通知」は「オン」です。誰でも私がそれを確認するのを手伝ってもらえますか?

MainActivity.class

package com.example.ww.lab; 

import android.app.Notification; 
import android.app.PendingIntent; 
import android.content.Intent; 
import android.support.v4.app.NotificationManagerCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.app.NotificationManager; 
import android.support.v4.app.NotificationCompat; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends AppCompatActivity { 
    private int id = 1; 
    private NotificationCompat.Builder notification_builder; 
    private NotificationManagerCompat notification_manager; 
    Button button; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     button = (Button) findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       notification_manager.notify(id, notification_builder.build()); 
      } 
     }); 

     Intent open_activity_intent = new Intent(this, NotificationActivity.class); 
     PendingIntent pending_intent = PendingIntent 
      .getActivity(this, 0, open_activity_intent, PendingIntent.FLAG_CANCEL_CURRENT); 

     notification_builder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle("Notification Title") 
      .setContentText("Notification Body") 
      .setAutoCancel(true) 
      .setContentIntent(pending_intent); 

     notification_manager = NotificationManagerCompat.from(this); 
    } 
} 

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" 
    tools:context="com.example.ww.lab.MainActivity"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Do it!" 
     android:id="@+id/button" 
     android:onClick="sendNotification"/> 
    </android.support.constraint.ConstraintLayout> 
+0

のAndroidメーカーご覧ください - ADKを使用:Nexus 5のAPI 26 - 使用API​​:API 21:アンドロイド5.0(ロリポップ) –

+0

あなたのコンパイルSDKおよびターゲットは何であるのアプリのビルドgradleファイルのSDK? –

+0

私はそれをテストし、エミュレータのNexus 5X API 24(アンドロイドN)を使って作業しています。 –

答えて

3

あなたコンパイル済みのSDKのバージョンを述べてきたように26です。投稿する前に通知のチャンネルを設定する必要があります。だから、より多くの場合

NotificationManager notification_manager = (NotificationManager) this 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 
    String chanel_id = "3000"; 
    CharSequence name = "Channel Name"; 
    String description = "Chanel Description"; 
    int importance = NotificationManager.IMPORTANCE_LOW; 
    NotificationChannel mChannel = new NotificationChannel(chanel_id, name, importance); 
    mChannel.setDescription(description); 
    mChannel.enableLights(true); 
    mChannel.setLightColor(Color.BLUE); 
    notification_manager.createNotificationChannel(mChannel); 
    notification_builder = new NotificationCompat.Builder(this, chanel_id) 
} else { 
    notification_builder = new NotificationCompat.Builder(this); 
} 
notification_builder.setSmallIcon(R.drawable.ic_launcher) 
     .setContentTitle("Notification Title") 
     .setContentText("Notification Body") 
     .setAutoCancel(true) 
     .setContentIntent(pending_intent); 

のようになりますDeveloper Site

+1

NotificationManagerCompatにcreateNotificationChannel()メソッドがないので、何か問題があります。ネイティブのNotificationManager – matdev

+2

を使用する必要があります。間違いを指摘してくれてありがとう。 –

関連する問題