为什么通知不起作用?

时间:2017-07-09 06:56:49

标签: android

例如,我想创建一个每5分钟有效的通知。为此,我使用BroadCastReceiver。这是我的代码:

   public void notifyMe()
   Calendar calendar = Calendar.getInstance() ;

    calendar.set(Calendar.HOUR,0);

    calendar.set(Calendar.MINUTE,5);

    calendar.set(Calendar.SECOND,0);


    Log.d("tagger",calendar.getTime().toString());



    Intent intent = new Intent(this,MyBroadcastReceiver.class);

    PendingIntent sender = PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);

    AlarmManager am = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);

    am.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),5*60*1000,sender);
   }

也适用于Receiver:

public class MyBroadcastReceiver extends BroadcastReceiver {

private static final int NOTIFY_ID = 101;



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

    Log.d("BroadcastReceiver", "debut receive");


    Intent resultIntent  =  new Intent(context,DetailActivity.class) ;

    PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
            0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification.Builder mBuilder = new Notification.Builder(context)
            .setContentTitle("title")
            .setContentText("some text");

    mBuilder.setAutoCancel(true);

    mBuilder.setContentIntent(resultPendingIntent);

    int id = 001 ;

    NotificationManager mNotifyManager = (NotificationManager)context.getSystemService(Application.NOTIFICATION_SERVICE);

    mNotifyManager.notify(id,mBuilder.build());
}
}

清单:

   <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:name=".PrefsApplication"
    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>
    <activity android:name=".DetailActivity" />
    <activity android:name=".ActionsActivity"

        ></activity>

    <receiver android:name=".MyBroadcastReceiver">



    </receiver>
</application>

它不起作用?我没有忘记在清单中添加接收器。我需要使用Service而不是Receiver吗?

2 个答案:

答案 0 :(得分:1)

试试这个:

 private void notificationManager(Context context, String message) {

    try {
        long when = System.currentTimeMillis();

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        Log.v("message", "," + message);

        Intent notificationIntent = new Intent(context, SplashActivity.class);


        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setAutoCancel(true);
        builder.setContentTitle(context.getString(R.string.app_name));
        builder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
        builder.setContentText(message);
        builder.setTicker(message);
        builder.setLights(Color.GREEN, 500, 500);
        builder.setWhen(when);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentIntent(intent);


        Notification notification = builder.build();
        notification.ledARGB = 0xFFff0000;
        notification.flags = Notification.FLAG_SHOW_LIGHTS;
        notification.ledOnMS = 100;
        notification.ledOffMS = 100;
        notificationManager.notify(1, notification);

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
        wl.acquire(15000);

    } catch (Exception e) {
        e.printStackTrace();
    }

}

希望这有帮助。

答案 1 :(得分:1)

更新notifyMe()方法,如下设置重复闹铃:

public void notifyMe()

    // Current time
    Calendar calendar = Calendar.getInstance() ;
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.SECOND, 0);

    Log.d("tagger", calendar.getTime().toString());

    Intent intent = new Intent(this, MyBroadcastReceiver.class);    
    PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    // Set alarm
    AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*60*1000, sender);

    // Or you can also use
    //am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*60*1000, sender);
}    

更新MyBroadcastReceiver,如下所示:

public class MyBroadcastReceiver extends BroadcastReceiver {

    private static final int NOTIFY_ID = 101;

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

        Log.d("BroadcastReceiver", "debut receive");

        // Intent to start activity 
        Intent resultIntent  =  new Intent(context, DetailActivity.class) ;
        PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
            0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification mNotification = new Notification.Builder(context)
            .setContentTitle("Title")
            .setContentText("Some text")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setAutoCancel(true)
            .setContentIntent(resultPendingIntent).build();


        NotificationManager mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotifyManager.notify(NOTIFY_ID, mNotification);
    }
}