1

Ich versuche, eine Benachrichtigung in der Benachrichtigungsleiste anzuzeigen, wenn ein Alarm ausgelöst wird. Unten ist mein Code.Android: Benachrichtigungen werden nicht in der Benachrichtigungsleiste angezeigt

AlarmReciever.java

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.telephony.SmsManager; 
import android.util.Log; 
import android.widget.Toast; 

/** 
* Created by Yohan on 6/29/2016. 
*/ 
public class AlarmReciever extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     // TODO Auto-generated method stub 


     // here you can start an activity or service depending on your need 
     // for ex you can start an activity to vibrate phone or to ring the phone 

     String message="Hi I will be there later, See You soon";// message to send 



     // Show the toast like in above screen shot 
     Log.d("Alarm",message); 
     Toast.makeText(context, "Alarm Triggered and SMS Sent", Toast.LENGTH_LONG).show(); 

     Intent intent2 = new Intent(context, NotificationReceiverActivity.class); 
     PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intent2, 0); 

     Notification noti = new Notification.Builder(context) 
       .setContentTitle("New mail from " + "[email protected]") 
       .setContentIntent(pIntent).getNotification(); 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     // hide the notification after its selected 
     noti.flags |= Notification.FLAG_AUTO_CANCEL; 

     notificationManager.notify(0, noti); 
    } 

} 

Here I getNotification() statt build() verwendet haben, ist, weil mein Minimum API 15.

NotificationReceiverActivity.java

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 

public class NotificationReceiverActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_notification_receiver); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 
    } 

} 

ist Manifest

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="voice1.xxx.com.alarmcheck2" > 

    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" /> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

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

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <receiver android:name=".BootCompletedReceiver" > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 
     <receiver 
      android:name=".AlarmReciever" 
      android:process=":remote" /> 

     <activity 
      android:name=".NotificationReceiverActivity" 
      android:label="@string/title_activity_notification_receiver" 
      android:theme="@style/AppTheme.NoActionBar" > 
     </activity> 
    </application> 

</manifest> 

In meinem AlarmReciever.java die Toast wird gefeuert, so weiß ich, es funktioniert. Die Benachrichtigung ist jedoch nicht. Irgendeine Idee warum es ist?

+2

Ich glaube, Sie vergessen das kleine Symbol der Benachrichtigung. Siehe hier: https://developer.android.com/guide/topics/ui/notifiers/notifications.html "Erforderliche Benachrichtigung Inhalt" – Nanis

+0

@ Nanis: Danke! Bitte geben Sie Ihren Kommentar als Antwort, damit ich akzeptieren kann! –

Antwort

8

Als Dokumentation gesagt, müssen Sie ein kleines Symbol setzen:

Erforderliche Benachrichtigung Inhalt Eine Benachrichtigung Objekt muss enthält die folgende:

Ein kleines Symbol, Set von setSmallIcon() Ein Titel, Set von setContentTitle() Text Detail, set von setContentText()

sehen Sie hier für weitere Informationen: https://developer.android.com/guide/topics/ui/notifiers/notifications.html

+0

Danke. +1 und Akzeptiert :) –

+0

funktioniert, wenn jemand von ihnen fehlt Benachrichtigung wird nicht angezeigt –