0

我试图在用户选择的特定时间内显示通知。我编写了代码,它运行良好,没有任何错误,但问题是通知未显示在设备中。我使用了在清单还权限..帮我解...通知未显示android

CustomNotification.class

public class CustomNotification extends Activity { 
     EditText etNotificationMessage; 
    String notificationMessage; 
    static final int TIME_DIALOG_ID = 0; 
    private int pHour, pMinute, pSeconds = 0; 
    private PendingIntent pendingIntent; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_custom_notification); 
     etNotificationMessage = (EditText) findViewById(R.id.etNotificationMessage); 
    } 

    public void setNotification(View v) { 
     notificationMessage = etNotificationMessage.getText().toString(); 
     Calendar c = Calendar.getInstance(); 
     c.set(Calendar.YEAR, c.get(Calendar.YEAR)); 
     c.set(Calendar.MONTH, c.get(Calendar.MONTH)); 
     c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH)); 
     c.set(Calendar.HOUR_OF_DAY, pHour); 
     c.set(Calendar.MINUTE, pMinute); 
     c.set(Calendar.SECOND, pSeconds); 
     /*Toast.makeText(
       getApplicationContext(), 
       c.get(Calendar.YEAR) + ":" + c.get(Calendar.MONTH) + 1 + ":" 
         + c.get(Calendar.DAY_OF_MONTH) + ":" + pHour + ":" 
         + pMinute + ":" + pSeconds, Toast.LENGTH_SHORT).show();*/ 
     Intent myIntent = new Intent(CustomNotification.this, MyReceiver.class); 
      pendingIntent = PendingIntent.getBroadcast(CustomNotification.this, 0, myIntent,0); 

      AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
      alarmManager.set(AlarmManager.RTC, c.getTimeInMillis(), pendingIntent); 
      Toast.makeText(
        getApplicationContext(),"Notification time is"+ 
        c.get(Calendar.YEAR) + ":" + c.get(Calendar.MONTH) + 1 + ":" 
          + c.get(Calendar.DAY_OF_MONTH) + ":" + pHour + ":" 
          + pMinute + ":" + pSeconds, Toast.LENGTH_SHORT).show(); 
    } 

    @SuppressWarnings("deprecation") 
    public void startTime(View v) { 
     /** Get the current time */ 
     final Calendar cal = Calendar.getInstance(); 
     pHour = cal.get(Calendar.HOUR_OF_DAY); 
     pMinute = cal.get(Calendar.MINUTE); 
     pSeconds = cal.get(Calendar.SECOND); 
     showDialog(TIME_DIALOG_ID); 

    } 

    @Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
     case TIME_DIALOG_ID: 
      return new TimePickerDialog(this, mTimeSetListener, pHour, pMinute, 
        false); 

     } 
     return null; 
    } 

    private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { 
     public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
      pHour = hourOfDay; 
      pMinute = minute; 

     } 
    }; 

} 

MyReceiver.class

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 

public class MyReceiver extends BroadcastReceiver 
{ 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     Intent service1 = new Intent(context, MyAlarmService.class); 
     context.startService(service1); 

    } 
} 

MyAlaramService.calss

public class MyAlarmService extends Service 
{ 

    private NotificationManager mManager; 

    @Override 
    public IBinder onBind(Intent arg0) 
    { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onCreate() 
    { 
     // TODO Auto-generated method stub 
     super.onCreate(); 
    } 

    @SuppressWarnings({ "static-access", "deprecation" }) 
    @Override 
    public void onStart(Intent intent, int startId) 
    { 
     super.onStart(intent, startId); 

     mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE); 
     Intent intent1 = new Intent(this.getApplicationContext(),CustomNotification.class); 

     Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis()); 
     intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent); 

     mManager.notify(0, notification); 
    } 

    @Override 
    public void onDestroy() 
    { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
    } 

} 

清单文件

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

    <uses-sdk 
     android:minSdkVersion="12" 
     android:targetSdkVersion="21" /> 

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

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/icon" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".WelcomeScreen" 
      android:label="@string/app_name" 
      android:screenOrientation="portrait" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name="com.om.notifications.CustomNotification" > 
     </activity> 
     <service 
      android:name=".MyAlarmService" 
      android:enabled="true" /> 
     <receiver android:name=".MyReceiver" /> 
    </application> 

</manifest> 
+0

尝试这个它会解决你的问题, [链接] [1] [1]:http://stackoverflow.com/a/30 452831/4447803 – Mano

+0

检查链接,它可能有助于解决你的问题,只是投票答案 – Mano

回答

0

把它写在你的广播接收器的onReceive()

NotificationCompat.Builder mBuild = new NotificationCompat.Builder(context) 
      .setSmallIcon(R.drawable.ic_launcher).setContentTitle("Notification") 
      .setContentText("New notification Received"); 
    Intent resultIntent = new Intent(context, CustomNotification .class); 
    resultIntent.setAction("FROMNOTIFICATION"); 
    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, 
      resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    mBuild.setContentIntent(resultPendingIntent); 
    NotificationManager mNotificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(0, mBuild.build()); 
    mBuild.setAutoCancel(true); 
+0

在那里你通过用户给的时间,我的意思是在什么时间通知显示 – saikrupa

+0

当广播接收机将收到推通知,那么只有你应该显示通知。 – Akash