1

api 25까지 구절에 완벽하게 작동합니다. 저는 Android를 처음 사용하고 Notification Alarm Manager를 사용하여 알림을 표시합니다.알림 관리자가 알림 API를 표시하지 않습니다. 26

누구든지 오류의 원인을 알 수 있습니까?

주요 활동 :

package br.exemploalarmmanagerbn; 

import java.util.Calendar; 

import android.app.Activity; 
import android.app.AlarmManager; 
import android.app.PendingIntent; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 

public class MainActivity extends Activity { 

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

     boolean alarmeAtivo = (PendingIntent.getBroadcast(this, 0, new Intent("ALARME_DISPARADO"), PendingIntent.FLAG_NO_CREATE) == null); 

     if(alarmeAtivo){ 
      Log.i("Script", "Novo alarme"); 

      Intent intent = new Intent("ALARME_DISPARADO"); 
      PendingIntent p = PendingIntent.getBroadcast(this, 0, intent, 0); 

      Calendar c = Calendar.getInstance(); 
      c.setTimeInMillis(System.currentTimeMillis()); 
      c.add(Calendar.SECOND, 3); 

      AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE); 
      alarme.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 5000, p); 
     } 
     else{ 
      Log.i("Script", "Alarme já ativo"); 
     } 
    } 

    @Override 
    public void onDestroy(){ 
     super.onDestroy(); 

     /*Intent intent = new Intent("ALARME_DISPARADO"); 
     PendingIntent p = PendingIntent.getBroadcast(this, 0, intent, 0); 

     AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE); 
     alarme.cancel(p);*/ 
    } 
} 

BroadcastReceiverAux

package br.exemploalarmmanagerbn; 

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.BitmapFactory; 
import android.media.Ringtone; 
import android.media.RingtoneManager; 
import android.net.Uri; 
import android.support.v4.app.NotificationCompat; 
import android.util.Log; 

public class BroadcastReceiverAux extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     Log.i("Script", "-> Alarme"); 

     gerarNotificacao(context, new Intent(context, MainActivity.class), "Nova mensagem", "Título", "Descrição nova mensagem"); 
    } 


    public void gerarNotificacao(Context context, Intent intent, CharSequence ticker, CharSequence titulo, CharSequence descricao){ 

if (Build.VERSION.SDK_INT >= 26) { 
      if (nm.getNotificationChannel(CHANNEL_ID) == null) { 
       nm.createNotificationChannel(new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, 
         NotificationManager.IMPORTANCE_DEFAULT)); 
      } 
     } 

     NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     PendingIntent p = PendingIntent.getActivity(context, 0, intent, 0); 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
     builder.setTicker(ticker); 
     builder.setContentTitle(titulo); 
     builder.setContentText(descricao); 
     builder.setSmallIcon(R.drawable.ic_launcher); 
     builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher)); 
     builder.setContentIntent(p); 

     Notification n = builder.build(); 
     n.vibrate = new long[]{150, 300, 150, 600}; 
     n.flags = Notification.FLAG_AUTO_CANCEL; 
     nm.notify(R.drawable.ic_launcher, n); 

     try{ 
      Uri som = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
      Ringtone toque = RingtoneManager.getRingtone(context, som); 
      toque.play(); 
     } 
     catch(Exception e){} 
    } 
} 

매니페스트

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="br.exemploalarmmanagerbn" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="18" /> 
    <uses-permission android:name="android.permission.VIBRATE"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="br.exemploalarmmanagerbn.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <receiver android:name="BroadcastReceiverAux" android:label="BroadcastReceiverAux"> 
      <intent-filter> 
       <action android:name="ALARME_DISPARADO"/> 
       <category android:name="android.intent.category.DEFAULT"/> 
      </intent-filter> 
     </receiver> 
    </application> 

</manifest> 

O código 케 postei abaixo ESTA funcionando perfeitamente, 마스 NAO posso afirmar 포 케 NAO 나 notifica?

+0

암시 API 26+ 당신은 명백한'Intent's를 사용하고 있었거나 했어야 했었습니다. '새로운 의도 ("ALARME_DISPARADO")'를'새로운 의도 (this, BroadcastReceiverAux.class)'로 변경하고, 매니페스트의''에서'<의도 필터>'를 제거하십시오. 또한 실제로 답변이 아니므로 아래에 게시 한 답변을 삭제하십시오. 추가 정보를 제공하려면 질문 아래의 태그 아래에있는 편집 링크를 사용하십시오. –

답변

1

당신은 IntentService에서 알림을 보내십시오

NotificationManager mNotificationManager = 
     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
// The id of the channel. 
String id = "my_channel_01"; 
// The user-visible name of the channel. 
CharSequence name = getString(R.string.channel_name); 
// The user-visible description of the channel. 
String description = getString(R.string.channel_description); 
int importance = NotificationManager.IMPORTANCE_HIGH; 
NotificationChannel mChannel = new NotificationChannel(id, name, importance); 
// Configure the notification channel. 
mChannel.setDescription(description); 
mChannel.enableLights(true); 
// Sets the notification light color for notifications posted to this 
// channel, if the device supports this feature. 
mChannel.setLightColor(Color.RED); 
mChannel.enableVibration(true); 
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); 
mNotificationManager.createNotificationChannel(mChannel); 


// The id of the channel. 
String CHANNEL_ID = "my_channel_01"; 
NotificationCompat.Builder mBuilder = 
     new NotificationCompat.Builder(this, CHANNEL_ID) 
      .setSmallIcon(R.drawable.notification_icon) 
      .setContentTitle("My notification") 
      .setContentText("Hello World!"); 
+0

나는 AlarmManager에 문제가 있다고 생각한다. '' –

+0

브로드 캐스트를 수신 할 수 있습니까? –

+0

은 api 25까지 작동합니다. –

0

notificatins 게시하려면 먼저 알림 채널을 만들고 그 채널을 사용해야합니다 :

  1. IntentService는 (그것을 선언하는 것을 잊지 마세요 만들기 매니페스트)
  2. gerarNotificacao() 메서드를 IntentService 클래스로 이동
  3. IntentService 내부에 브로드 캐스트 리시버의 onReceive() 메소드
  4. 에서 5,
  5. 실행 IntentService는 onHandleIntent에서 gerarNotificacao을()() 메서드를 호출
  6. Intent`s가 더 이상 (대부분의) 매니페스트 등록 된 수신기를 위해 작동하지`