【问题标题】:Notification not working after I close app关闭应用后通知不起作用
【发布时间】:2016-09-06 03:22:10
【问题描述】:

我正在开发一个 Android 应用程序,并且目前有通知在用户设定的时间和日期工作。

我的问题是,在我设置了时间和日期后,我从手机上关闭了应用程序。但是当我关闭应用程序时通知不起作用。

这是我的广播接收器类`

@Override
public void onReceive(Context context, Intent intent) {
    String text=intent.getStringExtra("param");
    List<String> content = Arrays.asList(text.split("\\s*,\\s*"));
    Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Intent intent1=new Intent(context.getApplicationContext(),HomeScreen.class);
    PendingIntent pIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, intent, 0);

    Notification notif=new Notification.Builder(context.getApplicationContext()).setContentTitle(content.get(0))
            .setContentText(content.get(1))
            .setSound(soundUri)
            .setContentIntent(pIntent)
            .setSmallIcon(R.mipmap.ic_launcherplannerlogo)
            .build();

    notif.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notif.defaults |= Notification.DEFAULT_SOUND;

    // Vibrate if vibrate is enabled
    notif.defaults |= Notification.DEFAULT_VIBRATE;

    NotificationManager notifMan=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    notifMan.notify(0,notif);



}

这是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.jacobabraham13.homeworkapplication">
    <!--<receiver android:process=":remote" android:name=".AlarmReceiver"
        android:permission="com.google.android.c2dm.permission.SEND"
    />--> <!--android:process=":remote" android:enabled="true"-->

    <receiver
        android:name=".AlarmReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.USER_PRESENT" />
            <category android:name="com.google.android.gcm.demo.app" />
        </intent-filter>
    </receiver>
    <service android:name=".GcmIntentService" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.android.alarm.persmission.SET_ALARM" />

<!--    <service android:name=".AlarmReceiver" android:enabled="true" android:exported="false">
        <intent-filter> <action android:name="NOTIFICATION_SERVICE" /></intent-filter>
    </service>-->

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ApplicationFunc.HomeScreen"
            android:label="@string/title_activity_home_screen"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity android:name=".ApplicationFunc.AddHomework" />
<!--        <service android:name=".ApplicationFunc.AlarmReceiver"
                 android:exported="false" />

        <activity android:name=".ApplicationFunc.ChooseTime"></activity>-->

    </application>


</manifest>

我在这里做错了什么?

请帮忙!!

`

【问题讨论】:

  • 您是否尝试过使用推送通知或服务? stackoverflow.com/questions/13741875/…
  • 我对如何实现这个有点困惑
  • Service 类是否应该是用户设置时间和日期的我的 Activity?

标签: java android notifications


【解决方案1】:

您应该将 PendingIntent 行替换为以下内容:

PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_NO_CREATE);

因为 PendingIntent.FLAG_NO_CREATE 标志表明所描述的 PendingIntent 是否已经不存在

【讨论】:

  • 不起作用是什么意思?没有出现在通知栏或打开后崩溃?
  • 通知没有在设定的时间出现。就像我上面说的,如果我不关闭应用程序并让它在后台运行,那么它就可以工作
【解决方案2】:
First Check When app is close OnReceive() method is Call or Not
if OnReceive() Method is calling, when app is close. try below Code. 


 PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
            .setSmallIcon(R.mipmap.ic_launcherplannerlogo)
            .setContentText(content.get(1))
            .setContentIntent(pIntent)
            .setAutoCancel(true)
            .notificationManager.notify(0, mBuilder.build());

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 2021-03-08
  • 2021-01-12
  • 1970-01-01
  • 1970-01-01
  • 2021-01-03
相关资源
最近更新 更多